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 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.

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 save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

3 minutes read.

Hibernate Annotation with 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 the specifications of the...

2 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 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.

Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values...

3 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 Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 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 Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

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 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.

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 Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.