Return value of JPA query when no matches found

From my little and personal experience, if you search for an object on your repo, for example by Id or Name the named query method returns an object of type T, but if no results are found from your repo, it will return null.

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

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

Appendix D: Repository query return types

Supported query return types
Query return types:

T An unique entity. Expects the query method to
return one result at most. In case no result is found null is
returned. More than one result will trigger an
IncorrectResultSizeDataAccessException.

Iterator An Iterator.

Seems like only when return type is of type T is the only one that specify a null is returned if no matches.

Leave a Comment

tech