Hibernate Query Language – HQL + Java 8 + MySQL

Hibernate provides a powerful language for queries: Hibernate Query Language (HQL). HQL is simmilar to SQL but it works with Java objects instead of tables. The tutorial shows you the advantages of HQL and how to work with it.

Related articles:
How to start development with Hibernate – XML Mapping File Hibernate

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Hibernate: 5.2.10.Final
– MySql database

II. Hibernate Query Language

HQL is an Object-Oriented Query language, simillar to database SQL. But instead of working with tables and columns, HQL uses Java Objects to manipulate queries.

How it work?
-> Hibernate converts HQL queries into appropriate SQL queries before processing on database.

Hibernate Query Language - HQL processing

Why uses HQL?
– Database Independent
– Take advantage of Hibernate caching.

HQL supports:
– FROM Clause
– AS Clause
– SELECT Clause
– WHERE Clause
– ORDER BY Clause
– GROUP BY Clause
– Using Named Paramters
– UPDATE Clause
– DELETE Clause
– INSERT Clause
– Aggregate Methods
– Pagination using Query

In the tutorial, we will practice with FROM, AS, SELECT, WHERE, ORDER BY, GROUP BY, Using Named Parameters, UPDATE Clause, DELETE.

1. FROM Clause

FROM clause is used to load all objects into memory.

2. AS Clause

AS clause is an optional keyword, used to create an aliases for a classes in HQL queries.

3. SELECT Clause

SELECT clause is used to obtain few properties of returned objects.

4. WHERE Clause

WHERE clause is used to narrow selected objects which must meet some conditions.

5. ORDER BY Clause

ORDER BY clause is used to sort HQL query’s results.

6. GROUP BY Clause

GROUP BY clause is used with aggregate functions.

7. Named Parameters

Named Parameters is used to get inputs from users and helpful to defend against SQL injection attacks.

8. UPDATE Clause

UPDATE clause is used to modify properties of objects.

9. DELETE Clause

DELETE clause is used to delete objects.

III. Practice

Step to do:
– Create Spring Maven project
– Create persistent class
– Setup Hibernate configuration
– Setup MySql Database
– Implement HQL Application
– Run & Check Results

1. Create Spring Maven project

Using Spring Tool Suite, create a Simple Spring Maven project.
Modified pom.xml file with Hibernate & MySql dependencies:

2. Create persistent class

Create a Customer.java:

3. Setup Hibernate configuration

Under /src/main/resources, create Hibernate Configuration file – hibernate.cfg.xml:

– Create a mapping file for Customer class – Customer.hbm.xml:

4. Setup MySql Database

Create a customer table and insert data to it with below SQL scripts:

5. Implement HQL Application

Implement 11 functions:
setUpSessionFactory(): setup Hibernate Session Factory
shutdown(): close Hiberbate Session Factory Object


6. Run & Check Results

– Initial customer table:

Hibernate Query Language - HQL - init customer table

– Run the main class Application.java, Logs:

– Database records after executing:

Hibernate Query Language - HQL - after processing customer table

IV. Sourcecode


Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *