Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change CrudRepository to return Optional<T> as return values instead of null reference or provide alternative base interface [DATACMNS-574] #1041

Closed
spring-projects-issues opened this issue Sep 20, 2014 · 5 comments
Assignees
Labels
type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 20, 2014

Marcel Härle opened DATACMNS-574 and commented

As described in the StackOverflow question, I think it would be a big improvement if the repositories returned an Optional<T> instead of possible null references when querying single objects.

Maybe you can consider this when you drop the support for JDK 6 and 7, and plan your next major release. Or you could introduce your own implementation for the Optional<T>, like the guava team did, when not dropping the JDK support for 6 and 7.

This change would only effect some of the CrudRepository interface methods. And the implementation is almost trivial


Reference URL: http://stackoverflow.com/questions/25883608/why-is-spring-data-still-using-null-references-as-return-values

Issue Links:

  • DATACMNS-603 First class suport for Optional

  • DATACMNS-944 Move to better naming scheme of methods in repository interfaces

1 votes, 5 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 21, 2014

Christoph Strobl commented

Optional<T> has already been introduced with DATACMNS-483

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 22, 2014

Oliver Drotbohm commented

If I read this correctly, Marcel was rather asking for the method signatures for CrudRepository to change, right?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 24, 2014

Marcel Härle commented

Yes. My intention is to get Optional<T> instead of T that might be null as documented in the Interface methods

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 27, 2014

Oliver Drotbohm commented

You can already build this yourself as you can see in my comment at DATACMNS-603. Changing CrudRepository in the 1.x timeframe is not an option as it'd break existing code. Even for 2.0 I am torn as constructs like this

if (repository.findOne(id) == null) { … }

would silently break without the user noticing. Shipping a dedicated OptionalCrudRepository would not work well in case people use PagingAndSortingRepository as you can't use them in combination then

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 12, 2017

Jens Schauder commented

This got fixed by DATACMNS-867

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants