Share on Google+Share on Google+

Hibernate Query Language

In this Tutorial we are going to discuss HQL with example.

Hibernate Query Language

In this Tutorial we are going to discuss HQL with example.

Hibernate Query Language :

Hibernate Query Language(HQL) is introduced by Hibernate. It is just like SQL except it is Object oriented query language.
As SQL operate on tables and columns as it is HQL interact with persistent objects and their properties.
Hibernate Queries is case insensitive ,except for names of Java classes and properties. It is like a bridge between database and application.

Reason to use HQL :

  1. It fully supports relational operations. It is similar to SQL except object oriented based.
  2. It returns result as object, which is easy to use.
  3. It supports polymorphic queries.
  4. It is very easy to learn and you can easily implement it in your application.
  5. HQL supports for advance features such as pagination, inner/outer/full joins, fetch join with dynamic profiling, Cartesian products.
  6. It also having feature to support projection, aggregation and grouping, ordering, sub queries and SQL function calls.
  7. Queries written in HQL are database independent.

Example :

In this example we are using HQL.Student is persistent class mapped to student table. We are selecting student such student whose roll number is greater than 2.

Our HQL is -

String hql = "FROM Student stud WHERE stud.roll > 2";

Main Class code :

package net.roseindia.main;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import java.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "FROM Student stud WHERE stud.roll > 2";
Query query = session.createQuery(hql);
List list = query.list();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student);
} catch (HibernateException e) {

} finally {


Hibernate: select student0_.roll_no as roll1_0_, student0_.course as course0_, as name0_ from student student0_ where student0_.roll_no>2

Download complete source code


Posted on: July 10, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Query Language  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image