Why is Spring Data still using null references as return values?

This is more a suggestion to improve than a question.

We all know about Tony Hoare's โ€œbillion dollar mistakeโ€ when inventing the zero link. Google Tips on the guava library wiki to avoid using null .

I really appreciate the Spring Data project, and we use Spring Data MongoDB in many projects. Is it likely that you will replace all possible null return reference values โ€‹โ€‹with Optional<T> ? I think this will be a big improvement when using Spring Data repository abstraction.

I know that many interfaces will need to be changed, but the code changes are almost trivial, just return the return type to Optional.of(returnValue) .

+6
source share
3 answers

Guava / JDK8 Optional<T> support has already been introduced with the RC1 Dijkstra exit train. Please check out spring -data-examples for java8 to find out how this works.

 interface CustomerRepository extends Repository<Customer, Long> { // CRUD method using Optional Optional<Customer> findOne(Long id); // Query method using Optional Optional<Customer> findByLastname(String lastname); } 

BTW: default methods are also supported there.

+10
source

I think that one day Spring Data may refuse to support JDK versions older than JDK 8, so itโ€™s quite simple to use JDK 8. However, since this will change the entire API, you will have to release a new major version, as well as upgrade paths, etc. (Changing the code would be trivial, but not the consequences for users of Spring data).

+1
source

With the latest and greatest versions of Spring Data, CrudRepository now actually returns`

+1
source

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


All Articles