Query.getSingleResult throws NoResultException

is normal when I call getSingleResult of EntityManager, if query doesn’t return any result, is fired an exception

NoResultException: getSingleResult() did not retrieve any entities.

is not better return null from getSingleResult ?


No, it is not better to return null. It is intended to throw NoResultException.

Use #getFirstResult() instead if you need null in case of empty result. All details are documented in JavaDoc.