HQL - Hibernate Query Language - Example Tutorial

HQL or Hibernate Query Language is the object-oriented query language of Hibernate Framework. HQL is very similar to SQL except that we use Objects instead of table names, that makes it more close to object oriented programming.

Hibernate Query Language – HQL

HQL and Case Sensitivity: HQL is case-insensitive except for java class and variable names. So SeLeCT is the same as sELEct is the same as SELECT, but com.journaldev.model.Employee is not same as com.journaldev.model.EMPLOYEE.

Some of the commonly supported clauses in HQL are:

  1. HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee. We can also create alias such as from Employee emp or from Employee as emp.
  2. HQL Join : HQL supports inner join, left outer join, right outer join and full join. For example, select e.name, a.city from Employee e INNER JOIN e.address a. In this query, Employee class should have a variable named address. We will look into it in the example code.
  3. Aggregate Functions: HQL supports commonly used aggregate functions such as count(*), count(distinct x), min(), max(), avg() and sum().
  4. Expressions: HQL supports arithmetic expressions (+, -, *, /), binary comparison operators (=, >=, , !=, like), logical operations (and, or, not) etc.
  5. HQL also supports ordre by and group by clauses.
  6. HQL also supports sub-queries just like SQL queries.
  7. HQL supports DDL, DML and executing store procedures too.

Let’s look at a simple example of using HQL in our program.

HQL Example Database Setup

I am using MySQL database for my example, below script will create two tables Employee and Address. They have one-to-one mapping and I am inserting some demo data for my example.

Create a maven project in Eclipse or the IDE you are using, our final project will look like below image.

Hibernate Query Language – HQL

Hibernate Maven Dependencies

Our final pom.xml contains dependencies for Hibernate and MySQL driver.

Hibernate Configuration XML

Our hibernate configuration xml file contains database connection related properties and mapping classes. I will be using annotations for Hibernate mapping.

hibernate.cfg.xml code:

Hibernate SessionFactory Utility class

We have a utility class to configure hibernate SessionFactory.

Model Classes with Annotation based mapping

Our model classes with JPA annotations looks like below.

HQL Example Test Class

Let’s see how to use HQL in java programs.

Notice that I am using HQL for Select, Update and Delete operations. It also shows how to use HQL Join and HQL Aggregate functions.

When I run above hql example program, we get following output.

Notice that once delete operation is performed, further operations are not showing that record data (sum of salary is 600). However I am rolling back the transaction, so the data in table will remain unchanged. Change the code to commit the transaction and it will be reflected in the database tables.

I don’t like using HQL query a lot because as you can see that we need to take care of table mappings in our code. If we will use Session to delete the Employee object, it will delete the record from both the tables.

You can download the sample hql example project from below link and try more examples.

By admin

Leave a Reply

%d bloggers like this: