Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




HQL

HQL

HQL stands for Hibernate Query Language. And as the name suggests, it is a query language which is very similar to SQL. But it has some advantages of its own.

Advantages of HQL

1. Instead of dealing with tables directly, we can write HQL using java objects/Entities.

2. A database join can be achieved with minimal code.

How to write HQL?

Let us take the Employee class :

Employee

class Employee {

   int id;
   String name;

   -- getters and setters --
}


There should be a simple mapping file for the same :


Employee.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name = "Employee" table = "EMPLOYEE">
   <id name = "id" type = "string">
     <column name = "ID">
    </id>
    <property name = "name" type = "string">
     <column name = "NAME">
    </property>
   </class>
</hibernate-mapping>


Now, let us write the main() class and save three 'Employee' objects to database.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Employee employeeJoe = new Employee();
    employee.setId(1);
    employee.setName("Joe");

    Employee employeePeter = new Employee();
    employee.setId(2);
    employee.setName("Peter");

    session.save(employeePeter);

    Employee employeeChang = new Employee();
    employee.setId(3);
    employee.setName("Chang");

    session.save(employeeChang);

    session.getTransaction().commit();

    session.close();
    sessionFactory().close();
  }
}

HQL SELECT Statement

Now, we will try to fetch some values from the 'EMPLOYEE' table using HQL SELECT.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
     session.beginTransaction();

     Query query = session.createQuery("from Employee");
     List <Employee> employeeList = query.list();

     for(Employee employee : employeeList) {

      System.out.println(employee.getId());
      System.out.println(employee.getName());
    }

     session.getTransaction().commit();

     session.close();
     sessionFactory.close();
   }
}

As we see above, we have written the actual query in the 'session.createQuery()' method and store the result in a 'Query' object.

Query query = session.createQuery("from Employee");

But the strange thing is, the 'Select' statement is missing from the 'Select' query.

'from Employee'

Well, Hibernate provides it. And the above statement means :

Select * from Employee

Now, it's up to you to write any of the above. Just don't forget 'Employee' is a java class and not a table. You just have to write the class name and Hibernate converts it to table for you.


HQL SELECT - WHERE Statement

Now, we will try to fetch some values from the 'EMPLOYEE' table using HQL SELECT and WHERE clause.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("from Employee employee WHERE employee.name='Joe'");

    session.getTransaction().commit();

    session.close();
    sessionFactory().close();
   }
}

Just like SQL we have written the WHERE clause

"from Employee employee WHERE employee.name='Joe'"

We have created an alias named 'employee' and used it to get the name 'Joe' in WHERE clause. As a result only 'Joe' is returned

Note : Similarly you can use WHERE, GROUP BY, ORDER BY along with the SELECT statement.