【发布时间】:2023-03-18 04:45:01
【问题描述】:
我正在尝试接收实体的 LocalDate 属性。但我得到了一个无法转换类型的异常。我正在尝试通过 Spring Boot JPA 文档搜索答案,但没有任何帮助。
Gradle 配置
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
实体
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Entity
@Table(name = "db_test")
public class TestEntity {
@Id
private String id;
private Integer age;
private LocalDate birthday;
}
存储库
@Repository
public interface TestRepository extends JpaRepository<TestEntity, String> {
@Query(nativeQuery = true, value = "SELECT DISTINCT birthday from db_test")
List<LocalDate> findAllBirthday();
}
测试方法
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestRepositoryTest {
@Autowired
private TestRepository testRepository;
@Test
public void findAllBirthday() {
TestEntity entity = new TestEntity("OK", 1, LocalDate.parse("2019-01-01"));
testRepository.save(entity);
List<LocalDate> result = testRepository.findAllBirthday();
Assert.assertEquals(1, result.size());
}
}
例外
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)
附加信息:
也许我没有很好地表达我的想法。这是我的操作:
- 使用 H2 作为默认数据库。
- 编写 JUnit4 测试方法。
- 在 Spring Boot 测试环境下运行测试。
- 希望有返回列表,但只有例外。
【问题讨论】:
-
尝试用
@Column(columnDefinition = "DATE")注释生日(最好的猜测来自 stackoverflow.com/questions/54840769/… 和 baeldung.com/jpa-java-time) -
这取决于你是如何定义你的表结构的。如果您使用的是 MySql 和 DATETIME 列。然后您必须使用 java.sql.Timestamp 来检索该信息,然后您可以根据您选择的 DateFormatter 进行转换
标签: java spring hibernate spring-boot jpa