In this tutorial, we are going to learn about Hibernate Query Language (HQL). HQL is mainly used to perform the bulk operations in hibernate.

In our Hibernate Tutorials, so far, we have executed CURD operations on a single object at a time. If we want to execute a set of operations at a time, we can go with bulk operations.

To perform bulk operations, we can use one of the following techniques.

  • Hibernate Query Language (HQL)
  • Hibernate Criteria API
  • Native SQL

In this tutorials, we are mainly concentrate about HQL operations.

Hibernate Query Language (HQL) :

  • HQL is a Hibernate’s own query language.
  • HQL is a Database independent query language, that is., using this query language, we can develop data base independent queries.
  • HQL queries are easy to learn, because HQL looks like SQL only.
  • We can easily translate the SQL commands into HQL commands, by replacing the table columns with pojo class variable names, and table name with pojo class name with reference variable.
  • HQL queries are also called as Object Oriented form of SQL queries.
  • At runtime Hibernate will convert the HQL query into SQL query according to the database. So we no need to write a query according to the database.

Hibernate Query for Select Operations :

In hibernate, we can divide the select operations into 2 types :

  • Reading Complete Entity
  • Reading Partial Entity

Reading Complete Entity :

In Hibernate, selecting the all columns is called reading a complete entity.

Example :

SQL : select * from emp;

HQL : from Employee e;

HQL commands to read a complete entity or entities are directly begins with “from” keyword.

Reading Partial Entity :

In Hibernate, reading a specific columns is called reading partial entity or entities.

Example :

SQL : select empno,sal from emp;

HQL : select e.employeeId, e.employeeSalary from Employee e;

In HQL, reading a partial entity is begins with “select” keyword.

HQL select with conditions :

In HQL we can pass the query parameters either in index parameters or named parameters.

Example :

SQL : select * from emp where deptno = ?;

HQL : from Employee e where e.deptNumber = ?


HQL : from Employee e where e.deptNumber =:deptNo;

[box type=”info” align=”alignleft” class=”” width=”100%”]Named parameters are indicated with “:”[/box]

Executing Select Query in HQL :

To perform the select operation, Hibernate given us Query object.

Query : A Query is a Object Orientation representation of Hibernate query. We can get the Query instance, by calling the session.createQuery();

We can also pass the parameters to the Hibernate Query. To do that, we can use query.setParameter(int pos, Object o)

And then call the list() on query object : query.list();

query.list() returns the query results in the form of java.util.List.

Example :

A simple HQL Query :

Query query = session.createQuery("from Employee e");

List qryResults = query.list();

HQL Query with parameters :

Query query = session.createQuery("from Employee e where e.deptNumber=?");


List qryResults = query.list();

Reading  the Complete Entity :

If the HQL command is to read the complete entity or entities then the list contains the complete objects. That means, Hibernate internally does like below:

  • Hibernate gets the data from the database and it stores the records of table in ResultSet object.
  • Each record of the ResultSet will be set to a pojo class object.
  • Adds each object of the pojo class to the java.util.List and finally returns the List.
Hibernate Query Complete Entity
Iterator it =list.iterator();
while (it.hasNext()) {
Employee emp = (Employee);
System.out.println("Employee Name : " + emp.getEmployeeName()
+ " , Salary : " + emp.getSalary());

Reading the partial entity :

If HQL command is to read partial entity or entities, then the list contains the Objects array (Object[]). That is the hibernate does internally like below:

  • Hibernate reads partial entities from table and stores the result in ResultSet.
  • Hibernate will set the data of each record in ResultSet to an Object array.
  • Each object[] will be added to the list.
  • Finally it returns the list.
Hibernate Query Partial Entity
Iterator it = list.iterator();

while (it.hasNext()) {
Object[] object = (Object[]);
System.out.println("Department Number : " + object[0]
+ " Salary : " + object[1]);

Reading partial entity with single column :

If the HQL command is to read partial entity with single column then the list contains Objects(Integer,String and etc.). That is., the Hibernate does internally like below:

  • Hibernate reads a single column of each record and stores in ResultSet.
  • Hibernate will set each record of ResultSet  into an Object of that property type (depends on the property type)
  • Adds objects to the list and finally returns the list.
Hibernate Query Single Entity
Iterator it = list.iterator();
while (it.hasNext()) {
String salary = (String);
System.out.println("Salary : " + salary);

This is how the Hibernate Select Query works. Download for complete example.

Happy learning 🙂

Download Example

About the Author:

Founder of Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

One Comment

  1. Milana Travis October 25, 2017 at 10:31 am - Reply

    Thank you very much for your blog.

    I enjoyed reading this article.

Leave A Comment