Я пытаюсь получить атрибут 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)
Дополнительная информация:
Может быть, я не express моя мысль хорошо. Вот моя операция:
- с использованием H2 в качестве базы данных по умолчанию.
- написание метода тестирования JUnit4.
- выполнение теста в тестовой среде с весенней загрузкой.
- надеюсь, что вернется список, но только получил исключение.