Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Query .list and .getResultList

Recently, If you are using Hibernate 5.2 or higher, then the Query::list() method has been deprecated.

Now, what the difference in using these two methods?

If anyone knows, please explain with examples.

like image 841
Sahil Aggarwal Avatar asked Sep 29 '17 19:09

Sahil Aggarwal


People also ask

What is query getSingleResult ()?

getSingleResult() Execute a SELECT query that returns a single untyped result. boolean.

Can getResultList return null?

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. Save this answer.


1 Answers

The documentation of Hibernate 3.2 says that Query#list() returns the query as List<T>.

Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

As you can read from the newer documentation of Hibernate 5.2 about the same named class and its method Query#getResultList is the overridden implementation of the the javax interface's method TypedQuery#getResultList.

Execute a SELECT query and return the query results as a typed List.

This method is a replacement of the one from the previous versions.

The idea is to implement Java EE interface (most of javax library) and keep the naming consistent.

like image 107
Nikolas Charalambidis Avatar answered Oct 03 '22 20:10

Nikolas Charalambidis