Spring JPA Data How to Use Kotlin Zero Values ​​Instead of Optional

I am writing a Spring Boot application with Spring JPA and Kotlin data, and I noticed that the CrudRepositoryfollowing method exists:

Optional<T> findById(ID id);

I use Kotlin, though, which has much faster ways to deal with zeros than Optional. Does anyone know how I will convert this method to work as follows?

fun findById(id: ID): T?

When I expand Repositoryand create a repo with this signature, I get an error:

java.lang.ClassCastException: java.util.Optional cannot be cast to com.books.Book
+13
source share
2 answers

Spring Data Lovelace SR4/Spring Boot 2.1.2, CrudRepository.findByIdOrNull(id: ID): T? = findById(id).orElse(null) CrudRepository.findByIdOrNull(id: ID): T? = findById(id).orElse(null) Kotlin CrudRepository.findByIdOrNull(id: ID): T? = findById(id).orElse(null) Spring Data.

Optional<T> Optional<T>, , findFooById(id: ID): T? . , , NULL, Optional<T> Optional<T>. , , .

. DATACMNS-1346 .

+18

12/2018:

Spring Data . , : . , .

:

, Optional Kotlin, - .

:

fun <T, ID> CrudRepository<T, ID>.findOne(id: ID): T? = findById(id).orElse(null)

:

val fruit: Fruit? = fruitRepository.findOne(id)

, .

+13

Source: https://habr.com/ru/post/1688830/


All Articles