4

Im trying to receive the LocalDate attribute of an entity. But I got a cannot convert type exception. And Im trying search answers by Spring Boot JPA document, but nothing helpful.

Gradle Config

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')
}

Configuration

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

Entity

@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Entity
@Table(name = "db_test")
public class TestEntity {
    @Id
    private String id;
    private Integer age;
    private LocalDate birthday;
}

Repository

@Repository
public interface TestRepository extends JpaRepository<TestEntity, String> {

    @Query(nativeQuery = true, value = "SELECT DISTINCT birthday from db_test")
    List<LocalDate> findAllBirthday();

}

Test Method

@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());
    }
}

Exception

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)


Additional information:

Maybe Im not express my thought well. Here is my operation:

  1. using H2 as default database.
  2. writing JUnit4 test method.
  3. running test under spring boot test environment.
  4. hoping there will return List, BUT ONLY GOT EXCEPTION.
kylooh
  • 41
  • 3
  • Try annotating birthday with `@Column(columnDefinition = "DATE")` (best guess from https://stackoverflow.com/questions/54840769/how-to-persist-localdate-with-jpa and https://www.baeldung.com/jpa-java-time ) – racraman Feb 17 '20 at 03:29
  • It depends how did you defined your table structure. In case you are using MySql and DATETIME Column. Then you must use java.sql.Timestamp to retrieve that information, then you might go for conversion as per your choice of DateFormatter – Swarit Agarwal Feb 17 '20 at 05:03

2 Answers2

0

How do you create fields in the database? If you're using MySQL your birthday field data type must as DATE! otherwise, this is expected because of casting.

Bhaumik Thakkar
  • 580
  • 1
  • 9
  • 28
  • I used `create-drop` strategy. Im trying to debug to find out. the return type is a List, and converting exception happened on List to List. Because JPA's conversion service is a static service object, and its converters not contains any Timestamp translate method. – kylooh Feb 18 '20 at 05:50
  • can you please verify me date format from your DB and type too? it might be YYYY-MM-DD – Bhaumik Thakkar Feb 18 '20 at 06:58
0

IT IS WILL NOT FIXED BY YOUR SELF!

Spring JPA convert database data by converters. And when you are querying for Timestamp data, there is no such convert for Timestamp to LocalDate as default.

Also JPA's converters not like Spring MVC's, custom converts will not added to JPA converts, it always using system default static ones.

kylooh
  • 41
  • 3