Newsletter |
Part 3 HQL, Different Ways Of Executing HQL Commands
Hibernate » on Jul 8, 2011 { 6 Comments } By Sivateja
We can execute our HQL command in 3 ways, like by selecting total object, partial object (more than one column), partial object (with single column). Let us see..
Different Ways Of Executing HQL
Case 1: [ Selecting Complete Object ]
- In this approach, we are going to select complete object from the database, so while iterating the collection, we need to typecast each object into our POJO class type only
- Internally hibernate converts each row selected from the table into an object of POJO class and hibernate stores all these POJO class objects into list so while iterating the collection, we typecast into POJO class type
Example:
Query qry = session.createQuery("-- our HQL command --"); List l =qry.list(); Iterator it = l.iterator(); while(it.hasNext()) { Object o = it.next(); Project p = (Product)o; ----- ------- ------ }
Case 2: [ Selecting Partial Object ]
- In this approach we are going to select partial object, (selected columns, i mean more than one column not single column)
- In this case hibernate internally stores the multiple column values of each row into an object array and stores these object arrays into List collection
- At the time of iterating the collection, we need to typecast the result into an object arrays
Example:
Query qry = session.createQuery("select p.pid,p.pname from Product p"); List l =qry.list(); Iterator it = l.iterator(); while(it.hasNext()) { Object o[] = (Object o[])it.next(); System.out.println("-------"); ----- ------- ------ }
Case 3: [ Selecting Partial Object ]
- In this case we are going to select partial object with single column from the database
- In this case hibernate internally creates an object of that value type and stores all these objects into the list collection
- At the time of iterating the collection, we need to typecast into that object type only
Example:
Query qry = session.createQuery("select p.pid from Product p"); List l =qry.list(); Iterator it = l.iterator(); while(it.hasNext()) { Integer i = (Integer)it.next(); System.out.println(i.intValue()); ----- ------- ------ }
Note: it.next() return type is always Object
Next Session HQL part 4
You Might Also Like
::. About the Author .:: | ||
Comments
6 Responses to “Part 3 HQL, Different Ways Of Executing HQL Commands”
It can’t be simpler than this. Great job buddy.
Awesome site. Really, learned in depth even though i worked these technologies. Simple explanation, motivates for java developer to learn.
Good one ..!
Good work Siva.!!
Great tutorial….
in Example one Project p = (Product)o .
i think its a Product p = (Product)o
Hi sir I could understand all the concepts very well. Thank you so much for this tutorials. It is very helpful and great explanation. I am lucky I saw this tutorials. very nice.