I\'m writing a Spring Boot app with Spring Data JPA and Kotlin, and I\'ve noticed that in CrudRepository there is the following method:
Optional
Update 12/2018:
An upcoming change in the Spring Data framework will make this answer obsolete. The update basically does the same as this answer: define an appropriate extension function. Please see Sébastien Deleuze's answer for further details.
Original answer:
As you correctly stated, you don't need Optional in Kotlin, because handling nullability in a concise manner is a build in language feature.
You could create your own extension function to achieve the desired behaviour:
fun CrudRepository.findOne(id: ID): T? = findById(id).orElse(null)
and use it like this:
val fruit: Fruit? = fruitRepository.findOne(id)
Thanks to Giordano who showed me a way to make the function more concise.