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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

6 Responses to “Part 3 HQL, Different Ways Of Executing HQL Commands”
  1. Alen says:

    It can’t be simpler than this. Great job buddy.

  2. Govind says:

    Awesome site. Really, learned in depth even though i worked these technologies. Simple explanation, motivates for java developer to learn.

  3. Ram says:

    Good work Siva.!!

  4. Nagarjuna says:

    Great tutorial….

    in Example one Project p = (Product)o .
    i think its a Product p = (Product)o

  5. Poornima says:

    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.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.