JPA request return value if no matches are found

I am using Spring JPA named querys in my repository. My problem is that I cannot find anywhere information about what will return a value for a query that will not match any results. I assume it will be null for findOne() , but I have no idea what it would be for the findAllByName() function.

Does anyone know from experience or know a place in the documentation?

+6
source share
1 answer

From my little personal experience, if you are looking for an object in your repo, for example, Id or Name, the named query method returns an object of type T , but if the results of your repo are not found, it will return null.

Methods that can return more than one item will create an empty List<T> (not null) collection.

Some documentation here: http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords

Appendix D: Repository Return Request Types

Supported return request types Return request types:

T Unique entity. Expects the query method to return one result maximum. If no result is found, null is returned equally. More than one result will raise an IncorrectResultSizeDataAccessException.

Iterator Iterator.

It seems that only when the type of the return type is of type T , is it the only one that indicates that null is returned if there are no matches.

+10
source

All Articles