This is more a suggestion for improvement than a question.
We all know about Tony Hoare's "billion dollar mistake" by inventing the null reference. Google advices in the wiki of the guava library to avoid using null.
I really appreciate the Spring Data project and we use Spring Data MongoDB in many projects. Is there a chance that you will replace all possible null reference return values by an Optional<T>
? I think this would be a big improvement when using the Spring Data repositories abstraction.
I know that a lot of the interfaces would have to be changed, but the code changes are almost trivial, just wrap the return type in an Optional.of(returnValue)
.
Guava/JDK8 Optional<T>
support has already been introduced with RC1 of Release Train Dijkstra. Please have a look at the spring-data-examples for java8 to see how it 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: there's support for default methods as well.
I think, once Spring Data can drop the support for JDK versions older than JDK 8, it should be fairly easy to use the JDK 8 Optional. However, since this would change the whole API, you would have to release a new major version, along with upgrade paths etc. (the code change would be trivial, but not the consequences for the users of Spring Data).
With the latest and greatest versions of Spring Data the CrudRepository now actually returns `Optional
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With