Introduction To Hibernate Query Language (HQL)

In this tutorial you will learn about Hibernate Query Language.

Introduction To Hibernate Query Language (HQL)

Introduction To Hibernate Query Language (HQL)

In this tutorial you will learn about Hibernate Query Language.

HQL is an abbreviation of Hibernate Query Language. It is a powerful query language, inspired by SQL that the Hibernate uses. When looking a HQL it is as similar to SQL but the HQL is different from SQL, HQL is fully object-oriented and it supports inheritance, polymorphism, abstraction. Queries in HQL are case-insensitive excluding the name of classes of java and the properties. for example in query WHerE is as same to whERe but a class name roseindia.net.HibernateExample is not same as roseindia.net.Hibernateexample.

Benefits of Hibernate Query Language is :

  1. Learning (HQL) Query in Hibernate is easy.
  2. HQL uses the (.) extension as it is used in object-orientation.
  3. Different SQL databases can be accessed by a single language provided by the HQL with a little different kind of SQL.
  4. HQL supports for relational operations. Using HQL a SQL query is allowed to be represented as an object form because in HQL rather tables and columns Classes and properties are used.
  5. HQL returns result as Object, this feature of HQL reduces the process of creation of an object and population of data from ResultSet.
  6. HQL provides database independent query writing.

Example :

In this example I will write a SQL query and a corresponding HQL query which will demonstrate you how a HQL is looking as same as SQL and how both are different. Let's suppose there is a table 'person' with their fields 'Id', 'Name', 'PersonType' having some value.

1. Writing a query in SQL

SELECT name FROM person WHERE id = "4"

2. A corresponding HQL query can be written as :

SELECT per.name FROM person per WHERE per.id = "4"

Some of the common HQL queries statements / clause are :

  • select
  • from
  • where
  • group by
  • order by
  • insert
  • update
  • delete

Each of the above statements / clause is explained individually. In the next section of this tutorial.