xenteros xenteros - 3 months ago 13x
Java Question

Return value of JPA query when no matches found

I'm using

Spring JPA named querys
in my repository. My problem is, that I can't find anywhere information what would be returned value for a query that wouldn't match any results. I assume it'll be null for
but I have no idea what would it be for

Does anyone know from his/her experience or know a place in documentation?


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.