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.
- Database-independent same HQL query works on different databases.
- Supports joins, aggregations, grouping, ordering, subqueries, etc.
- Supports inheritance queries can work on parent classes and include child class entities.
- Properties in HQL map directly to Java class fields, not column names.

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.
- Fetches full entity data
- Works with class names, not table names
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.
- Improves performance by selecting limited fields
- Returns partial data (columns/attributes)
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.
- Retrieves only matching data
- Supports operators (=, >, <, etc.)
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.
- Supports ASC (ascending) and DESC (descending)
- Applied on entity properties
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:
- Order By - DESC will sort in descending order
- Order By - ASC will sort in ascending order
5. UPDATE Clause
The UPDATE clause is required to _update the value of an attribute. Used to update entity data in the database.
- Modifies existing records
- Supports named parameters
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.
- Deletes specific rows based on condition
- Works without loading objects
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.
- Supports bulk insert operations
- Uses SELECT statement for values
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.
- **Query setMaxResults(int max): Instructs Hibernate to get a specific number of items.
- **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.
- Works on numeric data
- Returns single result
String hib = "SELECT AVG(marks) FROM Student";
Query q=session.createQuery(hib);
Max
To find the maximum __mark_among all the available marks.
- Useful for highest value queries
- Works on numbers/dates
String hib = "SELECT MAX(marks) FROM Student";
Query q=session.createQuery(hib);
Min
To find the minimum __mark_among all the available marks.
- Retrieves smallest value
- Works on numbers/dates
String hib = "SELECT MIN(marks) FROM Student";
Query q=session.createQuery(hib);
Count
To find the count of _id present.
- Returns total rows
- Often used with conditions
String hib = "SELECT COUNT(id) FROM Student";
Query q=session.createQuery(hib);
Sum
To find the sum of all the marks of Student.
- Works on numeric fields
- Returns aggregated result
String hib = "SELECT SUM(marks) FROM Student";
Query q=session.createQuery(hib);
List list=q.list();
System.out.println(list.get(0));