There is an alternative way provided by Hibernate to manipulate objects and in turn the data available in an RDBMS table. A Java programmer might feel it is easier to use Hibernate Criteria Query Language (HCQL) as it supports methods to add criteria on a query.
We can build a criteria object using the Criteria
interface, where we can apply logical conditions and filtration rules. The Session interface of Hibernate provides the createCriteria()
method to create a Criteria
object that returns an instance of a persistence object's class when your application executes a criteria query.
The following is a list of commonly used methods from the Criteria
interface:
Method |
Description |
---|---|
|
This method is used to add restrictions |
|
This method is used to specify ordering |
|
This method is used to specify the first number of record to be retrieved |
|
This method is used to specify the total number of records to be retrieved |
|
This method returns the list containing the object |
|
This method is used to specify the projection |
The following code snippet retrieves all the objects that correspond to the Employee
class using the criteria
query:
public List<Employee> getAllEmployees() { Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); List<Employee> emList = criteria.list(); return emList; }
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000] Employee [id=2, name=Shree Kant, jobTitle=Software Engineer department=Technology salary=3000]
Restrictive classes provide methods that we can use as Criteria
. Let's have a look at a few of them.
The eq
method will set the equal constraint to a given property.
The syntax of this method is:
public static SimpleExpression eq(String propertyName,Object value)
The following code snippet shows the use of the eq
method retrieving all the records of the Employee table whose salary is equal to 5000
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.eq("salary", 5000)); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.SALARY=? Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
This method sets the greater than constraint to a given property. The syntax of this method is:
public static SimpleExpression gt(String propertyName,Object value)
The following code snippet shows the use of the gt
method retrieving all the records of the Employee table whose ID is greater than 1
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.gt("id", 1)); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.ID>? Employee [id=2, name=Shree Kant, jobTitle=Software Engineer department=Technology salary=3000]
This method sets the less than constraint to a given property. The syntax of this method is:
public static SimpleExpression lt(String propertyName,Object value)
The following code snippet shows the use of the lt
method retrieving all the records of the Employee table whose id
is lesser than 3
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.lt("id", 2)); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.ID<? Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
This method sets the like
constraint to a given property. The syntax of this method is:
public static SimpleExpression like(String propertyName, Object value)
The following code snippet shows the use of the like
method retrieving all the records of the Employee table whose firstName
property is like RAVI
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.like("firstName", "RAVI")); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.FIRST_NAME like ? Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
This method sets the ilike
constraint to the given property and is case sensitive. The syntax of this method is:
public static SimpleExpression ilike(String propertyName, Object value)
The following code snippet shows the use of the ilike
method retrieving all the records of the Employee table whose firstName
property is like RAVI
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.ilike("firstName", "RAVI")); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.FIRST_NAME ilike ? Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
This method sets the between
constraint. The syntax of this method is:
public static Criterion between(String propertyName, Object low, Object high)
The following code snippet shows the use of the between
method retrieving all the records of the Employee table whose salary is between 4000
and 5000
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.between("salary", 4000,5000)); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.SALARY between ? and ? Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
This method sets the isNull
constraint to the given property. The syntax of this method is:
public static Criterion isNull(String propertyName)
The following code snippet shows the use of the isNull
method retrieving all the records of the Employee table whose salary is null
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.isNull("salary")); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.SALARY is null
This method sets the isNotNull
constraint to the given property. The syntax of this method is:
public static Criterion isNotNUll(String propertyName)
The following code snippet shows the use of the isNotNull
method retrieving all the records of the Employee table whose salary is not null
:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.add(Restrictions.isNotNull("salary")); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where this_.SALARY is not null Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000] Employee [id=2, name=Shree Kant, jobTitle=Software Engineer department=Technology salary=3000]
LogicalExpression
restrictions can be used to create AND
or OR
conditions as discussed in the following section.
The following code snippet shows the and
condition:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); Criterion salary = Restrictions.eq("salary", 5000); Criterion firstName = Restrictions.like("firstName", "RAVI"); LogicalExpression andExp = Restrictions.and(salary, firstName); criteria.add(andExp); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where (this_.SALARY=? and this_.FIRST_NAME like ?) Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]
The following code snippet shows the or
condition:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); Criterion jobTitle = Restrictions.eq("jobTitle", "AUTHOR"); Criterion firstName = Restrictions.like("lastName", "Kant"); LogicalExpression orExp = Restrictions.or(jobTitle, firstName); criteria.add(orExp); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ where (this_.JOB_TITLE=? or this_.LAST_NAME like ?) Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000] Employee [id=2, name=Shree Kant, jobTitle=Software Engineer department=Technology salary=3000]
HCQL supports pagination, where we can construct a paging component in our application. The Criteria
interface supports two methods for pagination:
The following code snippet will fetch two rows at a time:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.setFirstResult(0); criteria.setMaxResults(2); List<Employee> emList = criteria.list();
The org.hibernate.criterion.Order
class of the Criteria
API can be used to sort your results in either ascending or descending order, according to one of the objects' properties.
public static Order asc(String propertyName)
: This method applies the ascending order based on a given propertypublic static Order desc(String propertyName)
: This method applies the descending order based on a given propertyThe following code snippet will order the result by ID in descending order:
Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Employee.class); criteria.addOrder(Order.desc("id")); List<Employee> emList = criteria.list();
The expected output will be as follows:
Hibernate: select this_.ID as ID0_0_, this_.DEPARTMENT as DEPARTMENT0_0_, this_.FIRST_NAME as FIRST3_0_0_, this_.JOB_TITLE as JOB4_0_0_, this_.LAST_NAME as LAST5_0_0_, this_.SALARY as SALARY0_0_ from EMPLOYEE_INFO this_ order by this_.ID desc Employee [id=2, name=Shree Kant, jobTitle=Software Engineer department=Technology salary=3000] Employee [id=1, name=RAVI SONI, jobTitle=AUTHOR department=TECHNOLOGY salary=5000]