HCQL Hibernate Criteria Query Language
Hibernate Criteria Query Language (HCQL) is mainly used for further filetering records on the basis of logical conditions. HCQL works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in org.hibernate package. These interfaces and classes provide the properties and methods to perform operations of HCQL.
Note -> In HCQL, we can only execute the select query. It doesn’t allow to use other queries like an update, insert, and delete.
Advantages of HCQL
- Dynamic Queries- HCQL queries are good for executing dynamic queries, whereas HQL does not support dynamic queries.
- Pagination – HCQL supports the concept of pagination. Pagination is a technique which divides result set in pages.
- Database Independent- HCQL is also a database-independent query language. Queries written in HCQL can be run in any database.
- Projection Class – The projection class is available in org.hibernate.criterion.Projection package which can be used to get minimum, maximum, sum, count, and average of the property values.
Criteria Interface
The Criteria interface is part of org.hibernate package. It is used to retrieve entities by creating criterion objects. Based on given conditions, it is used to search objects or data.
There are many methods available in the Criteria interface to specify the criteria. Following are some methods of the Criteria interface:
Method | Description |
public Criteria add(Criteria c) | It is used to add restrictions on the result. |
public Criteria addOrder(Order o) | It is used to define the order of the result. Ascending or descending. |
public Criteria setFirstResult(int firstResult) | It is used to set the first record in the result |
public Criteria setMaxResult(int totalResult) | It is used to restrict maximum number of records |
public List list() | Returns the list of fetched records |
public Criteria setProjection(Projection projection) | Applies projection on the retrieved objects |
public Criteria createAlias(String associationPath, String alias) | Used to create alias on joined association |
Order Class
To sort the records of the query, we use Order class. Using this class, we can sort the records in ascending or descending order.
Following are the methods of Order class:
- public static Order asc(String propertyName)
Example of Order.asc
Crietria criteria=session.createCriteria(Employee.class);
criteria.addOrder(Order.asc("name"));
List list=criteria.list();
- public static Order desc(String propertyName)
Example of Order.desc
Crietria criteria=session.createCriteria(Employee.class);
criteria.addOrder(Order.desc("name"));
List list=criteria.list();
- public Order ignoreCase()
It is used to ignore the space, upper, and lower case in the record, i.e., it case-insensitive.
Restrictions Class
Restrictions class provides several methods which can be used to add restrictions to the criteria object. Following are the methods of org.hibernate.criterion.Restrictions package.
- public static SimpleExpression eq(String propertyName, Object value)
Here ‘eq’ stands for equal. It is used to apply an ‘equal to’ constraint on the given property.
Example of Restrictions.eq
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.eq("empId", 10));
List list = criteria.list();
- public static SimpleExpression ge(String propertyName, Object value)
‘ge’ is abbreviation for greater than or equal to.
Example of Restrictions.ge
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.ge("empId", 10));
List list = criteria.list();
- public static SimpleExpression gt(String propertyName, Object value)
‘gt’ stands for greater than.
Example of Restrictions.gt
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.gt("empId", 10));
List list = criteria.list();
- public static SimpleExpression le(String propertyName, Object value)
‘le’ stands for less than or equal to.
Example of Restrictions.le
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.le("empId", 10));
List list = criteria.list();
- public static SimpleExpression lt(String propertyName, Object value)
lt stands for less than.
Example of Restrictions.lt
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.lt("empId", 10));
List list = criteria.list();
- public static SimpleExpression like(String propertyName, Object value)
It is used to apply ‘like’ constraint on a property.
Example of Restrictions.like
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.like("name", "%Kumar%"));
List list = criteria.list();
- public static SimpleExpression lt(String propertyName, Object value)
Here ‘lt’ stands for less than. It is used to apply ‘less than’ constraint to the given property.
Example of Restrictions.lt
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.lt("empId", 10));
List list = criteria.list();
- public static SimpleExpression ne(String propertyName, Object value)
‘ne’ stands for not equal.
Example of Restrictions.ne
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.ne("empId", 10));
List list = criteria.list();
- public static Criterion between(String propertyName, Object obj1, Object obj2)
It is used to apply ‘between’ constraint between the two objects.
Example of Restrictions.between
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.between("empId", 100, 200));
List list = criteria.list();