Table of contents
1.
Introduction
2.
Hibernate Query Language
2.1.
List of Clauses
2.1.1.
FROM Clause
2.1.2.
AS Clause
2.1.3.
SELECT Clause
2.1.4.
WHERE Clause
2.1.5.
ORDER BY Clause
2.1.6.
GROUP BY Clause
2.1.7.
UPDATE Clause
2.1.8.
DELETE Clause
2.1.9.
INSERT Clause
2.2.
Aggregate Methods
2.2.1.
Avg Method
2.2.2.
Count Method
2.2.3.
Max Method
2.2.4.
Min Method
2.2.5.
Sum Method
3.
Frequently Asked Questions
3.1.
What are the advantages of using Hibernate Template?
3.2.
How many types of collections are there in Hibernate? Name them.
3.3.
Define callback interfaces.
3.4.
How many ORM levels are there in Hibernate? Name them.
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Hibernate Query Language

Author Rupal Saluja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Databases have now become a vital part of our lives. Every organization, whether private or public, commercial or social, maintains a database to store every small detail of their firm. When dealing with databases, we just cannot neglect the queries used to manage those databases, and there comes query language in use. By the name Query Language, we can guess that it is a language that makes the insertion, retrieval, and modification of the data in the databases easier. Hibernate Query Language, also known as HQL, is one such language. Using Hibernate Query Language is preferred so that portability issues can be solved, though native SQL can also be used.

Hibernate Query Language

Before learning about Hibernate Query Language, we need to know what is Hibernate. Hibernate is a framework of Java that makes the creation of database-interactive applications in Java convenient. Since HQL uses the class name instead of the table name unlike SQL, therefore, we say that HQL is a Query Language that is independent of databases. Also, it makes HQL closer to Object-Oriented Programming, so, Hibernate Query Language is also known as an object-oriented query language. It converts its own queries into SQL queries so that these queries can be used to perform database actions. The main advantage of Hibernate Query Language is that it is not case-sensitive except for the java class and its variable names.

List of Clauses

To learn about the clauses supported by Hibernate Query Language, refer to the following list.

FROM Clause

This clause is used to load the complete persistent object into memory. It works like the ‘Select *’ clause of SQL.

The syntax for using FROM clause is mentioned below.

String classhql = "FROM Student";
Query q = session.createQuery(classhql);
List output = q.list();

 

AS Clause

You may have heard of aliases while using SQL. To assign such aliases to the classes of Hibernate Query Language in queries, especially in case of lengthy queries, AS clause is used.

The syntax for using AS clause is mentioned below.

String classhql = "FROM Student AS S";
Query q = session.createQuery(classhql);
List output = q.list();

 

SELECT Clause

This clause is used when it is required to provide more control over the result set than the FROM clause. If you do not want to obtain the complete object but just a few properties of objects, use the SELECT clause. 

The syntax for using the SELECT clause is mentioned below.

String classhql = "SELECT S.firstName FROM Student S";
Query q = session.createQuery(classhql);
List output = q.list();

 

WHERE Clause

If you want to narrow down the properties of objects returned, that is, only some specific properties, then we will WHERE clause.

The syntax for using the WHERE clause is mentioned below.

String classhql = "FROM Student S WHERE S.id = 101";
Query q = session.createQuery(classhql);
List output = q.list();

 

ORDER BY Clause

This clause is used when you want to sort your query’s results. You can order your results using the reference of any object’s property either in descending or in ascending order.

The syntax for using the ORDER BY clause is mentioned below.

String classhql = "FROM Student S WHERE S.id > 101 ORDER BY S.salary DESC";
Query q = session.createQuery(classhql);
List output = q.list();

 

GROUP BY Clause

This clause groups the object’s properties taking the reference of a value of an attribute. The result generated is used to return an aggregate value of the objects.

The syntax for using the GROUP BY clause is mentioned below.

String classhql = "SELECT SUM(S.Marks), S.firstName FROM Employee S " + "GROUP BY S.firstName";
Query q = session.createQuery(classhql);
List output = q.list();

 

UPDATE Clause

To update one or multiple properties of one or multiple objects, we use the UPDATE clause. Certain changes in the object’s properties are done using this clause.

The syntax for using the UPDATE clause is mentioned below.

String classhql = "UPDATE Student set Marks = :Marks "  + "WHERE id = :student_id";
Query q = session.createQuery(classhql);
q.setParameter("Marks", 90);
q.setParameter("student_id", 101);
int output = q.executeUpdate();
System.out.println("Rows affected: " + output);

 

DELETE Clause

This clause deletes one or multiple objects. When an object is deleted, its properties are deleted along with it.

The syntax for using the DELETE clause is mentioned below.

String classhql = "DELETE FROM Student "  + "WHERE id = :student_id";
Query q = session.createQuery(classhql);
q.setParameter("student_id", 101);
int output = q.executeUpdate();
System.out.println("Rows affected: " + output);

 

INSERT Clause

This clause inserts one or multiple objects. When an object is inserted, its properties are inserted along with it.

The syntax for using the INSERT clause is mentioned below.

String classhql = "INSERT INTO Student(firstName, lastName, marks)"  + "SELECT firstName, lastName, marks FROM old_student";
Query q = session.createQuery(classhql);
int output = query.executeUpdate();
System.out.println("Rows affected: " + output);

Aggregate Methods

Similar to SQL, Hibernate Query Language facilitates the utilization of various Aggregate Methods. These methods work the same way in Hibernate Query Language as in SQL. To learn more about the various aggregate methods which are available in HQL, refer to the following information.

Avg Method

As the name suggests, it returns the average of any property’s value. Instead of calculating the average for each property, this method returns the average for the complete property. The syntax for this method is ‘avg(property name)’.

Count Method

The name of every method of Hibernate Query Language suggests its functions. The same applies to this method. It counts the number of times each property occurs in the results. The syntax for this method is ‘count(property name or *)’.

Max Method

The max method will perform its function as per its name, that is, it returns the maximum of all property values. The syntax for this method is ‘max(property value)’.

Min Method

The min method will also perform its function as per its name, that is, it returns the minimum of all property values. The syntax for this method is ‘min(property value)’.

Sum Method

This method returns the total sum of all the property values. Instead of calculating the sum for each property, this method returns the sum for the complete property. The syntax for this method is ‘sum(property name)’.

Learn more about Introduction to JQuery here.

Frequently Asked Questions

What are the advantages of using Hibernate Template?

The advantages of using Hibernate Template are given below.

  • Automated session closing.
  • Simplified interaction with hibernate session.
  • Automated exception handling.

How many types of collections are there in Hibernate? Name them.

There are two types of collections in Hibernate. These are, namely:

  • Sorted Collections
  • Order Collections

Define callback interfaces.

To receive event notifications from objects in Hibernate, callback interfaces are used. Suppose, when an object is getting loaded or deleted, an event will be generated. Then, the notification will be sent using callback interfaces.

How many ORM levels are there in Hibernate? Name them.

There are four ORM levels in Hibernate. These are, namely:

  • Pure Relational Mapping
  • Light Object Mapping
  • Medium Object Mapping
  • Full Object Mapping

Conclusion

In a nutshell, we understood what is Hibernate Query Language and learned about its various clauses and aggregate methods. We also saw the methods of the query interface that are used for Pagination.

We hope the above discussion helped you understand Hibernate Query Language in clearer terms and can be used for future reference whenever needed. For a crystal understanding of query language, you can refer to our blogs on Hibernate Interview QuestionsStructured Query LanguageLearning SQL and Database Languages

by clicking on the respective links.

To grasp more knowledge about MongoDBDatabasesOperational Databases and Non-Relational Databases, you can pay attention to our blogs on those topics by clicking on the respective links. Visit our website to read more such blogs. Make sure that you enroll in the courses provided by us, take mock tests and solve problems available and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Happy Coding!

Live masterclass