Table of contents
1.
Introduction
2.
Hibernate Criteria Query Language (HCQL)
3.
Advantages of Hibernate Criteria Query Language (HCQL)
4.
Criteria Interface
5.
Methods of Criteria Interface to specify the criteria 
6.
Restrictions Class
7.
Order Class
8.
Frequently Asked Questions
8.1.
What is ORM?
8.2.
What is HQL?
8.3.
What is Criteria API?
8.4.
What does Hibernate's lazy loading mean?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Hibernate Criteria Query Language (HCQL)

Author Ayush Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Databases are now an essential component of our daily life. We cannot ignore the queries that are used to manage databases while dealing with databases, and there is a query language in use.

In this blog, we will look at Hibernate criteria query language (HCQL) to manage the database, which fetches data based on specific criteria.

Hibernate Criteria Query Language (HCQL)

Before learning about Hibernate Criteria Query Language (HCQL), we must know Hibernate Query Language, also known as HQL. In contrast to SQL, which operates on tables and columns. It is an object-oriented query language that works on persistent objects and their properties. Hibernate converts HQL queries into standard SQL queries.

Hibernate Criteria Query Language (HCQL) searches for and retrieves records based on some criteria. It is used to utilize logical conditions and filtering rules to operate. The methods needed to carry out HCQL operations are provided by the Criteria Interface, Restrictions class, and Order class.

Also see, Introduction to JQuery

Advantages of Hibernate Criteria Query Language (HCQL)

  • It backs up the idea of pagination. It is a method for breaking up a large result set of pages into a manageable amount of pages.
  • It is a database-independent and object-oriented query language.
  • Hibernate Criteria Query Language supports dynamic queries, whereas HQL supports static queries.
  • Using Criteria API in Hibernate Criteria Query Lang, we may specify criteria query items using rules, filters, and logical conditions.
  • It supports the projection class, which is used to get the property values' sum, count, average, minimum, and maximum.

Criteria Interface

The Criteria interface is contained in the org.hibernate package, which is used to create criterion objects to retrieve entities. According to the requirements, it is used to search for objects or data.

Using the createCriteria() method of the Session interface, the criteria object may be obtained.

Syntax of createCriteria() method of Session interface

 public Criteria createCriteria(Class c)

Methods of Criteria Interface to specify the criteria 

 

1. public Criteria add(Criteria c): It is used to impose limitations on the result.

2. public Criteria addOrder(Order o): It is used to specify how the results should be sorted, such as ascending or descending.

3. public Criteria setFirstResult(int firstResult): It is used to specify the first number of the requested record.

4. public Criteria setMaxResult(int totalResult): It is used to estimate how many records will need to be retrieved altogether.

5. public List list(): It gives back a list of the most recent objects to be searched.

6. public Criteria createAlias(String associationPath, String alias): It is used to either assign an alias to the newly joined association or to join the association.

Restrictions Class

The restrictions class offers a variety of techniques to add restrictions to the criterion object in Hibernate Criteria Query Language (HCQL).

The most commonly used method of Restriction class are:

1. public static SimpleExpression lt(String propertyName, Object value): The provided property will have a "less than" constraint applied. “lt” stands for less than here.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.lt("EmployeeId", 250));
List list = criteria.list(); 

 

2. public static SimpleExpression le(String propertyName, Object value): It is used to apply the constraint "less than or equal" to the specified attribute.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.le("EmployeeId", 100));
List list = criteria.list();

 

3. public static SimpleExpression gt(String propertyName, Object value): It is used to impose a "greater than" limitation on the specified property.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.gt("EmployeeId", 50)); 
List list = criteria.list(); 

 

4. public static SimpleExpression ge(String propertyName, Object value): It is used to apply the constraint "greater than or equal" to the specified attribute.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.ge("EmployeeId", 50)); 
List list = criteria.list();

 

5. public static SimpleExpression ne(String propertyName, Object value): It is used to apply the constraint "not equal" to the specified property.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.ne("EmployeeId", 100)); 
List list = criteria.list();

 

6. public static SimpleExpression eq(String propertyName, Object value): It is used to impose an "equal" constraint on the specified property.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.eq("EmployeeId", 70)); 
List list = criteria.list();

 

7. public static Criterion between(String propertyName, Object low, Object high): It is used to apply a "between" constraint between the specified property's lo (low) and hi (high) object values.

Example:

Criteria criteria = session.createCriteria(Empolyee.class);
criteria.add(Restrictions.between("EmployeeId", 5,10));
List list = criteria.list(); 

 

8. public static SimpleExpression like(String propertyName, Object value):  It is used to impose a "like" constraint on the specified characteristic.

Example:



Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.like("EmployeeName", “%JAIN%”));
List list = criteria.list(); 

Order Class

Order class is used to sort the database table's records. We can order the records in ascending or descending fashion using the order class.

The most commonly used method of Order class are:

 1. public static Order asc(String propertyName): It is applied to sort the data in ascending order.

Example:

 Crietria criteria=session.createCriteria(Employee.class);  
 criteria.addOrder(Order.asc("EmployeeName"));  
 List list=criteria.list();   

 

2. public static Order desc(String propertyName): It is applied to sort the data in descending order.

Example:

 Crietria criteria=session.createCriteria(Employee.class);  
 criteria.addOrder(Order.desc("EmployeeName"));  
 List list=criteria.list();   

Frequently Asked Questions

What is ORM?

Object Relational Mapping is referred to as ORM. It is a programming paradigm that is used to persist Java objects to database tables.

What is HQL?

HQL is the query language that is an extension of SQL. It is a very efficient, simple, and flexible query language to do various operations on the relational database without writing complex queries.

What is Criteria API?

It is used to build queries for entities and their persistent state by developing query-defining objects. The Java programming language's APIs create type-safe, portable criteria queries.

What does Hibernate's lazy loading mean?

The lazy setting is utilized to decide whether to load the child objects while loading the parent object. The relevant hibernate mapping file for the parent class is where this setting needs to be made. The child objects' sluggish loading is enabled by default if true = lazy (meaning not to load child).

Conclusion

Thank you for completing the blog! The whole Hibernate criteria query language (HCQL) was addressed in this blog. In Hibernate criteria query language (HCQL), we've discussed the advantages of HCQL, Criteria API, Restrictions class, and Order class.

We hope that the Hibernate Criteria Query Language (HCQL) blog above helped you better understand it and will serve as a fruitful resource in the future.

You can refer to our blogs on Hibernate Interview QuestionsStructured Query LanguageLearning SQL, and Database Languages for a clear grasp of query language by clicking on the appropriate links.

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy Learning!

Live masterclass