Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Query and List for session.createQuery ?

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;



 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post full error stack trace.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic