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 vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 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.

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 Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 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.

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 save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 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 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 Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 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.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 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 Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 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 Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 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.