查询包含 java.sql.Date 列的表时,Jpa findAll() 导致错误
Posted
技术标签:
【中文标题】查询包含 java.sql.Date 列的表时,Jpa findAll() 导致错误【英文标题】:Jpa findAll() results in error when consulting a table that includes a java.sql.Date column 【发布时间】:2021-11-25 04:57:06 【问题描述】:您好,我正在大学学习 Java,并自学 Spring-Boot。这是我的第一个个人项目。我想从这个MariaDB Tutorial. 为示例国家数据库制作一个 Restful API 好吧,有一个国家表有一个“national_day”日期类型列,我怀疑它会导致问题。我创建了一个扩展 JpaRepository 的 @Repository 类,因此我可以使用 findAll() 和其他方法。之后,我做了一些控制台打印,以检查我是否能够获取数据,并且我在每次咨询时都得到了正确的答案,除非我尝试 findAll() 方法。我做了另一个测试,在national_day 列上方添加了@Transient 注释,findAll() 运行但national_day 列变为空值。 这些是我的java类: 国家实体
package me.givo.nationdbapiproject.model;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "countries")
public class Countries
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "country_id", length = 11, nullable = false)
private Integer country_id;
@Column(name = "name", length = 50, nullable = true)
private String name;
@Column(name = "area", nullable = false)
private BigDecimal area;
// @Transient
@Column(name = "national_day", nullable = true)
private java.sql.Date national_day;
@Column(name = "country_code2", length = 2, nullable = false)
private String country_code2;
@Column(name = "country_code3", length = 3, nullable = false)
private String country_code3;
public Integer getCountry_id()
return country_id;
public void setCountry_id(Integer country_id)
this.country_id = country_id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public BigDecimal getArea()
return area;
public void setArea(BigDecimal area)
this.area = area;
public java.sql.Date getNational_day()
return national_day;
public void setNational_day(java.sql.Date national_day)
this.national_day = national_day;
public String getCountry_code2()
return country_code2;
public void setCountry_code2(String country_code2)
this.country_code2 = country_code2;
public String getCountry_code3()
return country_code3;
public void setCountry_code3(String country_code3)
this.country_code3 = country_code3;
@Override
public String toString()
return "Countries [area=" + area + ", country_code2=" + country_code2 + ", country_code3=" + country_code3
+ ", country_id=" + country_id + ", name=" + name + ", national_day=" + national_day + "]";
@Override
public int hashCode()
final int prime = 31;
int result = 1;
result = prime * result + ((area == null) ? 0 : area.hashCode());
result = prime * result + ((country_code2 == null) ? 0 : country_code2.hashCode());
result = prime * result + ((country_code3 == null) ? 0 : country_code3.hashCode());
result = prime * result + ((country_id == null) ? 0 : country_id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((national_day == null) ? 0 : national_day.hashCode());
return result;
@Override
public boolean equals(Object obj)
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Countries other = (Countries) obj;
if (area == null)
if (other.area != null)
return false;
else if (!area.equals(other.area))
return false;
if (country_code2 == null)
if (other.country_code2 != null)
return false;
else if (!country_code2.equals(other.country_code2))
return false;
if (country_code3 == null)
if (other.country_code3 != null)
return false;
else if (!country_code3.equals(other.country_code3))
return false;
if (country_id == null)
if (other.country_id != null)
return false;
else if (!country_id.equals(other.country_id))
return false;
if (name == null)
if (other.name != null)
return false;
else if (!name.equals(other.name))
return false;
if (national_day == null)
if (other.national_day != null)
return false;
else if (!national_day.equals(other.national_day))
return false;
return true;
国家资料库
package me.givo.nationdbapiproject.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import me.givo.nationdbapiproject.model.Countries;
@Repository
public interface ICountriesJpaRepository extends JpaRepository<Countries, Integer>
// select fields from countries where name='[param]'
Countries findByName(String name);
我的测试
package me.givo.nationdbapiproject.repository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class ICountriesJpaRepositoryTest
@Autowired
private ICountriesJpaRepository repository;
@Test
public void countCountries()
System.out.println(repository.count());
System.out.println(repository.getById(167).getNational_day());
assertEquals(239, repository.findAll().size());
@Test
public void shouldGetAngola()
System.out.println("National day: " + repository.findByName("Angola").toString());
assertEquals("Angola", repository.findByName("Angola").getName(), "Not Angola!");
这是调试输出(不知道为什么是这样的格式,sy)更新:再粘贴一遍就可以得到正确的格式了。
20:22:35.018 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest]: ICountriesJpaRepositoryTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
20:22:35.063 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest]
20:22:35.098 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [/home/jose/Java_Projects/nation-db-api-project/target/classes/me/givo/nationdbapiproject/NationDbApiProjectApplication.class]
20:22:35.099 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration me.givo.nationdbapiproject.NationDbApiProjectApplication for test class me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest
20:22:35.102 [main] DEBUG org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - @TestExecutionListeners is not present for class [me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest]: using defaults.
20:22:35.102 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
20:22:35.115 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@247310d0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1033576a, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@303cf2ba, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@76494737, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4a003cbe, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4082ba93, org.springframework.test.context.transaction.TransactionalTestExecutionListener@17fc391b, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@2b30a42c, org.springframework.test.context.event.EventPublishingTestExecutionListener@609e8838, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@359df09a, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@43df23d3, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@6d60fe40, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@792b749c, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@23e84203, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@19932c16]
20:22:35.117 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@c430e6c testClass = ICountriesJpaRepositoryTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@312aa7c testClass = ICountriesJpaRepositoryTest, locations = '', classes = 'class me.givo.nationdbapiproject.NationDbApiProjectApplication', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = 'org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true', contextCustomizers = set[[ImportsContextCustomizer@536f2a7e key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6f27a732, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2cdd0d4b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@482bce4f, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4da4253, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@53cc893, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@66498326, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
20:22:35.128 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@c430e6c testClass = ICountriesJpaRepositoryTest, testInstance = me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest@6a8658ff, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@312aa7c testClass = ICountriesJpaRepositoryTest, locations = '', classes = 'class me.givo.nationdbapiproject.NationDbApiProjectApplication', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = 'org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true', contextCustomizers = set[[ImportsContextCustomizer@536f2a7e key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6f27a732, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2cdd0d4b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@482bce4f, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4da4253, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@53cc893, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@66498326, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]].
20:22:35.146 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: spring.jmx.enabled=false, org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.5)
2021-10-04 20:22:35.392 INFO 3021 --- [ main] m.g.n.r.ICountriesJpaRepositoryTest : Starting ICountriesJpaRepositoryTest using Java 17-ea on givo1 with PID 3021 (started by jose in /home/jose/Java_Projects/nation-db-api-project)
2021-10-04 20:22:35.395 INFO 3021 --- [ main] m.g.n.r.ICountriesJpaRepositoryTest : No active profile set, falling back to default profiles: default
2021-10-04 20:22:35.681 INFO 3021 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-10-04 20:22:35.724 INFO 3021 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38 ms. Found 3 JPA repository interfaces.
2021-10-04 20:22:36.031 INFO 3021 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-10-04 20:22:36.061 INFO 3021 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.5.7.Final
2021-10-04 20:22:36.140 INFO 3021 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.1.2.Final
2021-10-04 20:22:36.209 INFO 3021 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-10-04 20:22:38.874 INFO 3021 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-10-04 20:22:38.898 INFO 3021 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.mysql57Dialect
2021-10-04 20:22:39.254 INFO 3021 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-10-04 20:22:39.258 INFO 3021 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-10-04 20:22:39.556 INFO 3021 --- [ main] m.g.n.r.ICountriesJpaRepositoryTest : Started ICountriesJpaRepositoryTest in 4.409 seconds (JVM running for 4.971)
2021-10-04 20:22:39.976 INFO 3021 --- [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@c430e6c testClass = ICountriesJpaRepositoryTest, testInstance = me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest@6a8658ff, testMethod = countCountries@ICountriesJpaRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@312aa7c testClass = ICountriesJpaRepositoryTest, locations = '', classes = 'class me.givo.nationdbapiproject.NationDbApiProjectApplication', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = 'org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true', contextCustomizers = set[[ImportsContextCustomizer@536f2a7e key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6f27a732, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2cdd0d4b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@482bce4f, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4da4253, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@53cc893, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@66498326, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@8d810f2]; rollback [true]
Hibernate: select count(*) as col_0_0_ from countries countries0_
239
Hibernate: select countries0_.country_id as country_1_1_0_, countries0_.area as area2_1_0_, countries0_.country_code2 as country_3_1_0_, countries0_.country_code3 as country_4_1_0_, countries0_.name as name5_1_0_, countries0_.national_day as national6_1_0_ from countries countries0_ where countries0_.country_id=?
1821-11-28
Hibernate: select countries0_.country_id as country_1_1_, countries0_.area as area2_1_, countries0_.country_code2 as country_3_1_, countries0_.country_code3 as country_4_1_, countries0_.name as name5_1_, countries0_.national_day as national6_1_ from countries countries0_
2021-10-04 20:22:40.612 WARN 3021 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S1009
2021-10-04 20:22:40.613 ERROR 3021 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : DAY_OF_MONTH
2021-10-04 20:22:40.848 INFO 3021 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@c430e6c testClass = ICountriesJpaRepositoryTest, testInstance = me.givo.nationdbapiproject.repository.ICountriesJpaRepositoryTest@6a8658ff, testMethod = countCountries@ICountriesJpaRepositoryTest, testException = org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query, mergedContextConfiguration = [MergedContextConfiguration@312aa7c testClass = ICountriesJpaRepositoryTest, locations = '', classes = 'class me.givo.nationdbapiproject.NationDbApiProjectApplication', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = 'org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true', contextCustomizers = set[[ImportsContextCustomizer@536f2a7e key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6f27a732, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2cdd0d4b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@482bce4f, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4da4253, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@53cc893, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@66498326, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]
2021-10-04 20:22:40.864 INFO 3021 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-10-04 20:22:40.865 INFO 3021 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-10-04 20:22:42.440 INFO 3021 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
我在 mysqlworkbench 上尝试了休眠查询,它选择了所有国家:
Hibernate: select countries0_.country_id as country_1_1_, countries0_.area as area2_1_, countries0_.country_code2 as country_3_1_, countries0_.country_code3 as country_4_1_, countries0_.name as name5_1_, countries0_.national_day as national6_1_ from countries countries0_;
任何帮助将不胜感激。谢谢!
【问题讨论】:
第一个问题很好。我很难过,这个例外似乎没有多大帮助。我确实有一个指针给你,但不幸的是它不会解决你的问题。数据库的正确大小写是您正在使用的snake_case,但对于Java,您应该使用纯camelCase 而不是您现在拥有的混合(JPA 将为您处理转换),因此例如它应该是private java.sql.Date nationalDay;
和@ 987654328@
谢谢,我担心使用与数据库中不同的名称会出错。我会全部改掉的。
【参考方案1】:
好的,我发现问题出在哪里。正如我上面提到的,我正在使用MariaDB Tutorial 数据库“Nation”。好吧,我将它部署在 GCP SQL 实例上,并将 SQL DRIVE 添加到我的项目中,而不是 MariaDB 中。现在我刚刚将 MariaDB DRIVE 添加到我的项目中,并从我的 application.properties 更改了驱动程序类名称,最后一切都很好!谢谢!
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
【讨论】:
以上是关于查询包含 java.sql.Date 列的表时,Jpa findAll() 导致错误的主要内容,如果未能解决你的问题,请参考以下文章
java.util.Date 到 java.sql.Date 不包含时间