Hibernate Query Language (original) (raw)

Last Updated : 23 Apr, 2026

Hibernate Query Language (HQL) is an object-oriented query language used to interact with databases through Hibernate . Unlike SQL, it works with entity classes and their properties instead of tables and columns. It is database-independent, as Hibernate converts HQL into SQL automatically.

hql

hql

The Query interface provides object-oriented methods and capabilities for representing and manipulating HQL queries.

**Note: HQL keywords (FROM, SELECT, WHERE) are case-insensitive, but entity class names and property names are case-sensitive.

Hibernate Query Language (HQL) Clauses

There are many HQL clauses available to interact with relational databases and several of them are listed below:

1. FROM Clause

To load a whole persistent object into memory. Used to retrieve complete objects (entities) from the database.

String hib = "FROM Student";
Query query = session.createQuery(hib, Student.class);
List results = query.list();

2. SELECT Clause

The SELECT clause is used when only a _few attributes of an object are required rather than the entire object. Used to fetch specific properties instead of the whole object.

String hib = "SELECT s.name FROM Student s";
Query query = session.createQuery(hib, String.class);
List results = query.list();

3. WHERE Clause

Filtering records is done with the WHERE clause. It's used to _retrieve only the records that meet a set of criteria.

String hib = "FROM Student S WHERE S.id = 5";
Query query = session.createQuery(hib, Student.class);
List results = query.list();

4. ORDER BY Clause

The ORDER BY clause is used to _sort the results of an HQL query.

String hib = "FROM Student S WHERE S.id > 5 ORDER BY S.id DESC";
Query query = session.createQuery(hib, Student.class);
List results = query.list();

**Note:

5. UPDATE Clause

The UPDATE clause is required to _update the value of an attribute. Used to update entity data in the database.

String hib = "UPDATE Student set name=:n WHERE roll=:i";
Query query = session.createQuery(hib, Student.class);
query.setParameter("n","John");
query.setParameter("i",23);
int status=q.executeUpdate();
System.out.println(status);

6. DELETE Clause

Deletes rows (entities) from the database based on a condition.

String hib = "DELETE FROM Student WHERE id=10";
Query query = session.createQuery(hib, Student.class);
query.executeUpdate();

7. INSERT Clause

It is required to _Insert values into the relation.

String hib = INSERT INTO Student(firstName, lastName)
SELECT s.firstName, s.lastName FROM BackupStudent s
Query query = session.createQuery(hib);
int result = query.executeUpdate();

For pagination using query we have **two methods available for it, below table contains the methods and its description.

  1. **Query setMaxResults(int max): Instructs Hibernate to get a specific number of items.
  2. **Query setFirstResult(int starting_no): Takes an integer as an argument that represents the first row in your result set, beginning with row 0.

To fetch the few row data at a time we can use pagination using HQL. For this example we are fetching the rows 5 to 10.

String hib = "FROM Student";
Query query=session.createQuery(hib);
query.setFirstResult(5);
query.setMaxResults(10);
List list=query.list();

The above example Returns 10 records starting from index 5 (i.e., rows 6 to 15, assuming zero-based indexing).

Aggregate Methods

Similar to SQL, HQL has several aggregation techniques, some of them are mentioned below-

Average

To find the average of **marks.

String hib = "SELECT AVG(marks) FROM Student";
Query q=session.createQuery(hib);

Max

To find the maximum __mark_among all the available marks.

String hib = "SELECT MAX(marks) FROM Student";
Query q=session.createQuery(hib);

Min

To find the minimum __mark_among all the available marks.

String hib = "SELECT MIN(marks) FROM Student";
Query q=session.createQuery(hib);

Count

To find the count of _id present.

String hib = "SELECT COUNT(id) FROM Student";
Query q=session.createQuery(hib);

Sum

To find the sum of all the marks of Student.

String hib = "SELECT SUM(marks) FROM Student";
Query q=session.createQuery(hib);
List list=q.list();
System.out.println(list.get(0));