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




HQL - Parameters passing

Say at times we don't want to pass the actual value to the WHERE clause. Rather we would prefer to pass it as a parameter.

Query query = session.createQuery("from Employee employee WHERE employee.name= :nameParam");
query.setString("nameParam","Joe");

We will see in detail, how to achieve the above in Hibernate.

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

How to Parameterize values in WHERE clause?

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= :nameParam");
    query.setSring("nameParam","Joe");

    session.getTransaction().commit();

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

In the above SELECT we have not put the actual name (i.e. Joe) in the WHERE clause. Instead we have created a variable 'nameParam'

Query query = session.createQuery("from Employee employee WHERE employee.name= :nameParam");

and initialized it with 'Joe' in the 'query.setSring(...)' method.

query.setSring("nameParam","Joe");