• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between Query and List for session.createQuery ?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post full error stack trace.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic