Can javax.persistence.Query.getResultList() return null?

JavaHibernateJpa

Java Problem Overview


And if so, under what circumstances?

Javadoc and JPA spec says nothing.

Java Solutions


Solution 1 - Java

You are right. JPA specification says nothing about it. But Java Persistence with Hibernate book, 2nd edition, says:

> If the query result is empty, a null is returned

Hibernate JPA implementation (Entity Manager) return null when you call query.getResultList() with no result.

UPDATE

As pointed out by some users, it seems that a newest version of Hibernate returns an empty list instead.

An empty list is returned in Eclipselink as well when no results are found.

Solution 2 - Java

If the specs said it could't happen, would you belive them? Given that your code could conceivably run against may different JPA implementations, would you trust every implementer to get it right?

No matter what, I would code defensively and check for null.

Now the big question: should we treat "null" and an empty List as synonymous? This is where the specs should help us, and don't.

My guess is that a null return (if indeed it could happen) would be equivalent to "I didn't understand the query" and empty list would be "yes, understood the query, but there were no records".

You perhaps have a code path (likely an exception) that deals with unparsable queries, I would tend to direct a null return down that path.

Solution 3 - Java

Contrary to Arthur's post, when I actually ran a query which no entities matched I got an empty list, not null. This is using Hibernate and is what I consider correct behaviour: an empty list is the correct answer when you ask for a collection of entities and there aren't any.

Solution 4 - Java

If you take a close look at the org.hibernate.loader.Loader (4.1) you will see that the list is always initialized inside the processResultSet() method (doc, source).

protected List processResultSet(...) throws SQLException {
   final List results = new ArrayList();

   handleEmptyCollections( queryParameters.getCollectionKeys(), rs, session );
   ...
   return results;

}

So I don't think it will return null now.

Solution 5 - Java

Of course, if you test the result set with Jakarta's CollectionUtils.isNotEmpty, you're covered either way.

Solution 6 - Java

Query.getResultList() returns an empty list instead of null. So check isEmpty() in the returned result, and continue with the rest of the logic if it is false.

Solution 7 - Java

Given the implementation of getResultsList() in org.hibernate.ejb.QueryImpl class, it is possible to return a null :

public List getResultList() {
	try {
		return query.list();
	}
	catch (QueryExecutionRequestException he) {
		throw new IllegalStateException(he);
	}
	catch( TypeMismatchException e ) {
		throw new IllegalArgumentException(e);
	}
	catch (HibernateException he) {
		em.throwPersistenceException( he );
		return null;
	}

My hibernate version is: 3.3.1.GA

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrdkView Question on Stackoverflow
Solution 1 - JavaArthur RonaldView Answer on Stackoverflow
Solution 2 - JavadjnaView Answer on Stackoverflow
Solution 3 - JavaAndrew SimonsView Answer on Stackoverflow
Solution 4 - JavaCharles FolletView Answer on Stackoverflow
Solution 5 - JavaAl SchererView Answer on Stackoverflow
Solution 6 - JavaCyril SojanView Answer on Stackoverflow
Solution 7 - Javawile the coyoteView Answer on Stackoverflow