Hibernate Query Language
Hibernate Query Language
Shubhangi Shinde
Introduction
Hibernate Query Language (HQL) is same as
SQL (Structured Query Language) but it
doesn't depends on the table of the database.
Instead of table name, we use class name in
HQL. So it is database independent query
language.
Advantage of HQL
database independent
supports polymorphic queries
easy to learn for Java Programmer
Query Interface
The query interface provides many methods. There is given
commonly used methods:
public int executeUpdate() is used to execute the update or
delete query.
public List list() returns the result of the ralation as a list.
public Query setFirstResult(int rowno) specifies the row
number from where record will be retrieved.
public Query setMaxResult(int rowno) specifies the no. of
records to be retrieved from the relation (table).
public Query setParameter(int position, Object value) it
sets the value to the JDBC style query parameter.
public Query setParameter(String name, Object value) it
sets the value to a named query parameter.
Example of HQL to get all the records
Query query=session.createQuery("from Emp
");//here persistent class name is Emp
List list=query.list();
Example of HQL to get records with
pagination
Query query=session.createQuery("from Emp
");
query.setFirstResult(5);
query.setMaxResult(10);
List list=query.list();//will return the records fr
om 5 to 10th number
Example of HQL update query
Transaction tx=session.beginTransaction();
Query q=session.createQuery("update User set n
ame=:n where id=:i");
q.setParameter("n","Udit Kumar");
q.setParameter("i",111);
int status=q.executeUpdate();
System.out.println(status);
tx.commit();
Example of HQL delete query
Query query=session.createQuery("delete fro
m Emp where id=100");
//specifying class name (Emp) not tablename
query.executeUpdate();
HQL with Aggregate functions
You may call avg(), min(), max() etc. aggregate
functions by HQL.
Example to get total salary of all the employees
Query q=session.createQuery("select sum(salary)
from Emp");
List<Emp> list=q.list();
Iterator<Emp> itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
Example to get maximum salary of employee
Query q=session.createQuery("select max(salary)
from Emp");
Example to get minimum salary of employee
Query q=session.createQuery("select min(salary)
from Emp");
Example to count total number of employee ID
Query q=session.createQuery("select count(id) fr
om Emp");
Example to get average salary of each employees
Query q=session.createQuery("select avg(salary) f
rom Emp");
HCQL (Hibernate Criteria Query
Language)
The Hibernate Criteria Query Language (HCQL)
is used to fetch the records based on the
specific criteria. The Criteria interface provides
methods to apply criteria such as retreiving all
the records of table whose salary is greater
than 50000 etc.
Advantage of HCQL
The HCQL provides methods to add criteria, so
it is easy for the java programmer to add
criteria. The java programmer is able to add
many criteria on a query.
Criteria Interface
The Criteria interface provides many methods
to specify criteria. The object of Criteria can
be obtained by calling
the createCriteria() method of Session
interface.
Syntax of createCriteria() method of Session interface
public Criteria createCriteria(Class c)
The commonly used methods of Criteria interface are as follows:
public Criteria add(Criterion c) is used to add restrictions.
public Criteria addOrder(Order o) specifies ordering.
public Criteria setFirstResult(int firstResult) specifies the first
number of record to be retreived.
public Criteria setMaxResult(int totalResult) specifies the total
number of records to be retreived.
public List list() returns list containing object.
public Criteria setProjection(Projection projection) specifies the
projection.
Restrictions class
Restrictions class provides methods that can be used as Criterion.
The commonly used methods of Restrictions class are as follows:
public static SimpleExpression lt(String propertyName,Object
value) sets the less thanconstraint to the given property.
public static SimpleExpression le(String propertyName,Object
value) sets the less than or equal constraint to the given property.
public static SimpleExpression gt(String propertyName,Object
value) sets the greater thanconstraint to the given property.
public static SimpleExpression ge(String propertyName,Object
value) sets the greater than or equal than constraint to the given
property.
public static SimpleExpression ne(String
propertyName,Object value) sets the not
equalconstraint to the given property.
public static SimpleExpression eq(String
propertyName,Object value) sets the equal constraint
to the given property.
public static Criterion between(String propertyName,
Object low, Object high) sets thebetween constraint.
public static SimpleExpression like(String
propertyName, Object value) sets the like constraint
to the given property.
Order class
The Order class represents an order. The
commonly used methods of Restrictions class
are as follows:
public static Order asc(String
propertyName) applies the ascending order
on the basis of given property.
public static Order desc(String
propertyName) applies the descending order
on the basis of given property.
Example of HCQL to get all the records
Crietria c=session.createCriteria(Emp.class);//passing Class class argument
List list=c.list();
Example of HCQL to get the 10th to 20th record
Crietria c=session.createCriteria(Emp.class);
c.setFirstResult(10);
c.setMaxResult(20);
List list=c.list();
Example of HCQL to get the records whose salary is greater than 10000
Crietria c=session.createCriteria(Emp.class);
c.add(Restrictions.gt("salary",10000));//salary is the propertyname
List list=c.list();
Example of HCQL to get the records in
ascending order on the basis of salary
Crietria c=session.createCriteria(Emp.class);
c.addOrder(Order.asc("salary"));
List list=c.list();
HCQL with Projection
We can fetch data of a particular column by
projection such as name etc. Let's see the
simple example of projection that prints data
of NAME column of the table only.
Criteria c=session.createCriteria(Emp.class);
c.setProjection(Projections.property("name"))
;
List list=c.list();