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.
getSingleResult() Execute a SELECT query that returns a single untyped result. boolean.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With