Forums Register Login

Difference between Query and List for session.createQuery ?

+Pie Number of slices to send: Send
Hi,

Difference Between return type used over here Query and List.

1) Query query = session.createQuery("SQL_QUERY") ;

2) List list = session.createQuery("SQL_QUERY").list() ;

When to use which one and why ?


+Pie Number of slices to send: Send
Query object is fundamentally a wrapper around our SQL/HQL query. Also it has various methods to sustitute values inside the actual query we've written.
Check the methods which are available in the query object. It's not just session.createQuery("SQL_QUERY").list() you can do.
There are lot of jdbc specific functions available.

List on the other hand just fires the query and gives you a collection object.
+Pie Number of slices to send: Send
I was Getting error while error while iterating over the query or List why so.



Error: could not iterate query.

or some times it gives me hashcode while i used toString() while using List;



+Pie Number of slices to send: Send
Please post full error stack trace.
+Pie Number of slices to send: Send
my code is working fine with no error but using List or query giving me the same result. So my question is when to use which one.


if you know any general case where we can make difference and use better option on both.


I am also sharing another example using HQL , only difference is here we have to use Actual Object while in case of SQL query use Object.



List contains iterator() method while query contains iterate() . what else ?
This tiny ad is guaranteed to be gluten free.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 12643 times.
Similar Threads
Hibernate
Please check my query
Difference between Session Cache for load and query cache ?
Query Difference?
Hibernate Date mapping
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:23:28.