code: 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();
    }
docs: Executes the query and ensures that either a single result is returned or null.
  
  @param query the query to be executed.
  @return a single result of any, otherwise null.
  @throws javax.persistence.NonUniqueResultException if more than one result is returned by the
  query.