Java 有没有办法接收列表<;LocalDate>;通过Spring Boot JPA存储库
我正在尝试接收实体的LocalDate属性。但是我遇到了一个无法转换类型的异常。我正在尝试通过Spring Boot JPA文档搜索答案,但没有任何帮助 渐变配置Java 有没有办法接收列表<;LocalDate>;通过Spring Boot JPA存储库,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我正在尝试接收实体的LocalDate属性。但是我遇到了一个无法转换类型的异常。我正在尝试通过Spring Boot JPA文档搜索答案,但没有任何帮助 渐变配置 plugins { id 'org.springframework.boot' version '2.2.0.RELEASE' id 'io.spring.dependency-management' version '1.0.7.RELEASE' id 'java' } configurations {
plugins {
id 'org.springframework.boot' version '2.2.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
id 'java'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
implementation group: 'com.h2database', name: 'h2', version: '1.4.200'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
配置
spring:
datasource:
driver-class-name: org.h2.Driver
username: sa
password: sa
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
hikari:
maximum-pool-size: 100
jpa:
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create-drop
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [@org.springframework.data.jpa.repository.Query java.util.List<java.time.LocalDate>] for value '[2019-01-01, 2019-01-02, 2019-01-03]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.sql.Date] to type [@org.springframework.data.jpa.repository.Query java.time.LocalDate]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:212)
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:166)
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:77)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
实体
@Getter
@诺尔格构装师
@AllArgsConstructor
@EqualsAndHashCode
@实体
@表(name=“db_测试”)
公共类测试{
@身份证
私有字符串id;
私人整数年龄;
私人生日;
}
存储库
@存储库
公共接口测试存储库扩展了JpaRepository{
@查询(nativeQuery=true,value=“从db\u测试中选择不同的生日”)
列出findAllBirthday();
}
试验方法
@SpringBootTest
@RunWith(SpringRunner.class)
公共类TestRepositoryTest{
@自动连线
私有测试库测试库;
@试验
公共无效findAllBirthday(){
TestEntity实体=新的TestEntity(“OK”,1,LocalDate.parse(“2019-01-01”);
testRepository.save(实体);
List result=testRepository.findAllBirthday();
Assert.assertEquals(1,result.size());
}
}
例外情况
spring:
datasource:
driver-class-name: org.h2.Driver
username: sa
password: sa
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
hikari:
maximum-pool-size: 100
jpa:
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create-drop
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [@org.springframework.data.jpa.repository.Query java.util.List<java.time.LocalDate>] for value '[2019-01-01, 2019-01-02, 2019-01-03]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.sql.Date] to type [@org.springframework.data.jpa.repository.Query java.time.LocalDate]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:212)
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:166)
at org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:77)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
org.springframework.core.convert.ConversionFailedException:未能将值“[2019-01-012019-01-022019-01-03]”从类型[java.util.ArrayList]转换为类型[@org.springframework.data.jpa.repository.Query java.util.List];嵌套异常为org.springframework.core.convert.ConverterNotFoundException:未找到能够从类型[java.sql.Date]转换为类型[@org.springframework.data.jpa.repository.Query java.time.LocalDate]的转换器
位于org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
位于org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
位于org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:212)
位于org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:166)
位于org.springframework.data.repository.core.support.QueryExecutionResultHandler.postProcessInvocationResult(QueryExecutionResultHandler.java:77)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
其他信息: 也许我不能很好地表达我的想法。以下是我的操作:
如何在数据库中创建字段?如果您使用的是MySQL,您的生日字段数据类型必须为DATE!
否则,由于铸造,这是预期的。它不会由您自己修复 SpringJPA通过转换器转换数据库数据。当您查询时间戳数据时,没有将时间戳转换为LocalDate作为默认值
此外,JPA的转换器与Spring MVC不同,自定义转换不会添加到JPA转换中,它始终使用系统默认的静态转换。尝试使用
@Column(columnDefinition=“DATE”)
(和的最佳猜测)注释生日,这取决于您是如何定义表结构的。如果您使用的是MySql和DATETIME列。然后您必须使用java.sql.Timestamp来检索该信息,然后您可以根据您选择的DateFormatterI usedcreate drop
策略进行转换。我正试图调试以找出答案。返回类型是一个列表,将列表上发生的异常转换为列表。因为JPA的转换服务是一个静态服务对象,并且它的转换器不包含任何时间戳转换方法。您能从您的数据库和类型中验证我的日期格式吗?可能是YYYY-MM-DD