public static Object getSingleResultOrNull ( Query query ) { List results = query . getResultList ( ) ; if ( results . isEmpty ( ) ) { return null ; } else if ( results . size ( ) == 1 ) { return results . get ( 0 ) ; } throw new NonUniqueResultException ( ) ; }
Executes the query and ensures that either a single result is returned or null .