Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in org.hibernate package provides the properties and methods to perform operations of HCQL.

The object of Criteria interface can be obtained by calling the method createCriteria() provided by the Session interface. In HCQL, we can only execute the select query. It doesn’t allow to use other queries like an update, insert, and delete.

Syntax of the Criteria query

Criteria criteria = session.createCriteria(persistantClassName.class);

Advantages of Hibernate Criteria Query Language (HCQL)

  • Dynamic Queries- HCQL queries are suitable for executing dynamic queries, whereas HQL does not support dynamic queries. It only supports static queries.
  • Pagination – Hibernate criteria query language (HCQL) supports the concept of pagination. Pagination is a technique which divides the fetching of a result set containing several pages into a small number of pages. 
  • Database Independent- HCQL is also a database-independent query language. That means if we write programs using HCQL command, the program can be executed in all relational databases without any modification.
  • Support Projection- 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 made available in org.hibernate package which is used to retrieve entities by creating criterion objects. It is used for searching objects or data as per the given conditions.

There are many methods available in the Criteria interface to specify the criteria. Following are some methods of the Criteria interface:

  1. public Criteria add(Criteria c)

It is used to add restrictions on the result.

  • public Criteria addOrder(Order o)

It is used for defining the order of the result such as ascending and descending.

  • public Criteria setFirstResult(int firstResult)

 It is used to define the first number of the record to be fetched.

  • public Criteria setMaxResult(int totalResult)

It is used to determine the total number of record to be retrieved.

  • public List list()

It returns the list of the current searched objects.

  • public Criteria setProjection(Projection projection)

It is used to apply projection to retrieve objects.

  • public Criteria createAlias(String associationPath, String alias)

It is used to join the association or assigning the alias to the joined association.

Order Class

Order class helps in sorting the records of the database table. By using order class, we can sort the records in ascending or descending order.

Following are the methods of Order class:

  • public static Order asc(String propertyName)

It is used to sort the data in ascending order.

Example of Order.asc

 Crietria criteria=session.createCriteria(Student.class);  
 criteria.addOrder(Order.asc("name"));  
 List list=criteria.list();   
  • public static Order desc(String propertyName)

It is used to sort the data in descending order.

Example of Order.desc

Crietria criteria=session.createCriteria(Student.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 many methods that can be used to add restrictions to the criteria object. There are many methods available in org.hibernate.criterion.Restrictions package.

Following are the commonly used methods of Restrictions class:

  1. public static SimpleExpression eq(String propertyName, Object value)

Here ‘eq’ stands for equal. It is used to apply an ‘equal’ constraint to the given property.

 Example of Restrictions.eq

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.eq("rollNum", 16)); 
List list = criteria.list(); 
  • public static SimpleExpression ge(String propertyName, Object value)

Here ‘ge’ stands for greater than or equal. It is used toapply ‘greater than or equal’ constraint to the given property.

Example of Restrictions.ge

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.ge("rollNum", 10));
List list = criteria.list(); 
  • public static SimpleExpression gt(String propertyName, Object value)

Here ‘gt’ stands for greater than. It is used to apply ‘greater than’ constraint to the given property.

Example of Restrictions.gt

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.gt("rollNum", 10)); 
List list = criteria.list();  
  • public static SimpleExpression le(String propertyName, Object value)

Here ‘le’ stands for less than or equal. It is used to apply ‘less than or equal’ constraint to the given property. 

Example of Restrictions.le

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.le("rollNum", 16));
List list = criteria.list(); 

  • public static SimpleExpression like(String propertyName, Object value)

  It is used to apply ‘like’ constraint to the given property.

Example of Restrictions.like

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.like("sname", "%SHARMA%"));
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(Student.class);
criteria.add(Restrictions.lt("rollNum", 10));
List list = criteria.list(); 
  • public static SimpleExpression ne(String propertyName, Object value)

Here ‘ne’ stands for not equal. It is used to apply ‘not equal’ constraint to the given property.

Example of Restrictions.ne

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.ne("rollNum", 7));
List list = criteria.list(); 
  • public static Criterion between(String propertyName, Object lo, Object hi)

It is used to apply ‘between’ constraint between the lo (low) and hi (high) object values of the given property.

Example of Restrictions.between

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.between("rollNum", 10,16));
List list = criteria.list(); 

Related Topics

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

3 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.