Hibernate Query Language and Executing HQL Commands
Let us see, how to execute HQL commands..
Procedure To Execute HQL Command:
- If we want to execute execute an HQL query on a database, we need to create a query object
- ” Query ” is an interface given in org.hibernate package
- In order to get query object, we need to call createQuery() method in the session Interface
- Query is an interface, QueryImpl is the implemented class
- we need to call list method for executing an HQL command on database, it returns java.util.List
- we need to use java.util.Iterator for iterating the List collection
Syntax:
123456789Query qry = session.createQuery("--- HQL command ---");
List l = qry.list();
Iterator it = l.iterator();
while(it.hasNext())
{
Object o = it.next();
Product p = (Product)o;
----- ------- ---------
}
Notes:
- line 1: Getting the Query object
- line 2: Executing the object content (which is HQL command)
- line 3: Iterating, then while loop and type cast into our class type that’s it




post a comment