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(); 

Related Topics

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of the...

2 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Hibernate vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Annotation Example in Hibernate

Hibernate Annotation Example Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases, and it also uses...

3 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Composite Primary Key

Hibernate Composite Primary Key A Composite Primary Key is a combination of one or more columns that forms a primary key. When a database table contains more than one primary key column, it is...

8 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Architecture of Hibernate

Next → Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc.  There are mainly three layers in Hibernate...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.