Hibernate Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and case insensitive query language. Also, it is easy to learn and supports concepts like polymorphism, inheritance, association, etc.

Advantages of HQL

  • Database Independent- HQL is a database-independent query language. That means if we write programs using HQL commands, then the program can be executed in all relational databases without any modification.
  • Case- insensitive for keywords- HQL is similar to SQL. Hence, their properties are also identical. HQL is case-insensitive for keywords like SQL, which means all the keywords can be written in both upper and lower case. For example, select, SELECT, and Select are all the same.
  • Object-oriented- HQL is an object-oriented query language as it supports inheritance, polymorphism, and association. Instead of real table names and columns, it uses class and property names.
  • Easy to Learn- HQL is easy to learnand implement, as its syntax is identical to SQL.

Syntax of HQL

HQL is the most strong query language designed as the object-oriented extension to SQL.  It is easy to understand and learn, and it uses class name and properties instead of real tables and column names.

Following are the components of HQL:

  1. Clauses (from, select, where, order by, group by)
  2. Joins and associations (inner join, left outer join, right outer join, full join)
  3. Aggregate functions (average, minimum, maximum, count, sum)
  4. Expressions (mathematical operators, binary comparison operator, logical operator, string concatenation)
  5. Sub-queries (any, all, some, in)

HQL Select query

A select statement is used to select data stored in the database. It is the most commonly used Data Manipulation Language (DML) command. If you want to know the properties of the object, use select command.

Example

String hql = "Select student.name From Student student";
Query query = session.createQuery(hql);
List results = query.list(); 

HQL update query

An update statement is used to modify several records as per the given condition. The update command is very powerful as it can alter a large number of records at the same time.

Example

String hql = "Update Students set rollno =:rollno"  + "WHERE id = :Sid";
Query query = session.createQuery(hql);
query.setParameter("rollno", 01624002016);
query.setParameter("Sid", 16);
int result = query.executeUpdate(); 

HQL insert query

An insert statement is used to insert new records in the table present in the database. It is a widely-used Data Manipulation Language (DML) command.

Example

String hql = "Insert into Students (name, course, rollno, Sid)";
Query query = session.createQuery(hql); 
int result = query.executeUpdate(); 

HQL delete query

Delete command is used to delete existing records in the table. It is also a widely-used Data Manipulation Language (DML) command.

Example

String hql = "Delete From Students WHERE id =:Sid";
Query query = session.createQuery(hql);
query.setParameter("Sid", 16);
int result = query.executeUpdate();