Hibernate Query Language Introduction
Posted By : Ganesh Chandra Tiwari | 16-Dec-2017
HQL (Hibernate Query Language) :-
HQL is an Object based Query language which is used by hibernate for loading entities.
To execute HQL queries, Query object uis required
Query object is created with the help of following method of Session.
public Query createQuery(String HQLQuery);
Methods for executing queries :-
(i) list()
Given query is executed and a list of selected object is returned.
public List list();
(ii) iterator()
Return an iterator to traverse the object selected by the given query.
public Iterator iterator();
Difference between list and iterators
list() is based on eager loading when this method is executed all the entities are selected which are represented by the queries.
iterator() provides the implmentation of lazy loading when this method is invoked only the id of selected object os loaded from the database. Each time next object is loaded from the database using its id.
1. To load all objects of a class
"from ClassName "
Eg.
Query q=session.createQuery("from Emp e");
2. To conditionally load object of a class.
"from ClassName alias a condition "
Eg.
Query q=session.createQuery("from Emp e where e.salary > 8000 ");
List<Emp> list=q.list();
Note : HQL queries support positional as well as named parameter.
To set the value of parameters, setter methods are provided by the Query object.
Eg.
To Load those Emp object salary of >=the given salary.
Positional Parameter
1. Query q=session.createQuery("from Emp e where e.salary >=?1");
Eg.
q.setString(1000); // ?1
q.setString("Ganesh"); // ?2
List<Emp> list =q.list();
Named Parameter
2. Query q=session.createQuery("from Emp e where e.salary >=:sal");
Eg.
int s=1000;
q.setInteger("sal",s);
List<Emp> list =q.list();
Aggregate function such as max() and min() count() etc. can also be used in a HQL Query.
(1) To load those Emp object salary of which is equal to the max salary.
Query q=session.createQuery("from Emp e where e.salary = max(e.salary)");
(2) To load those Emp objects who have two or more priviliges.
Query q=session.creatQuery("from Emp e where size(e.priviliges)>=2 ");
"Order by alias.propertyName" is used to select object in ascending order of the specified property.
"Order by alias.propertyName desc" is used to select object in descending order of the specified property.
(1) To load all Emp object im ascending order of their name.
Query q=session.createQuery(" from Emp e order by e.name ");
(2) To load all Emp object im descending order of their name.
Query q=session.createQuery(" from Emp e order by desc e.name ");
To select only some fields of objects
"select alias.fieldName1,alias.fieldName2 from ClassName alias ..."
When a single field is selected a list of string is returned.
When multiple fields are selected, a list of array of object is returned list() method is overloaded.
(1) To load only the name of the Emp object.
Query q=session.createQuery("select e.name , e.salary from Emp e");
List<Emp> list =q.list();
(2) To load name and cost of all the previliges.
Query q =session.createQuery("select p.name, p.cost from priviliges p");
List<Object[]) list=q.list();
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ganesh Chandra Tiwari
Ganesh is a Web Application Developer. He has knowledge of Spring, Struts, Hibernate, Angular JS, HTML, CSS, Javascript.