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




Hibernate- Criteria


Criteria

Criteria is another way of writing queries in Hibernate. The advantage is, it is a lot more object oriented or java friendly.

A simple 'SELECT * FROM EMPLOYEE' statement would be as simple as :


Criteria criteria = session.createCriteria(Employee.class);


Adding Restrictions to Criteria

To add restriction to a query in SQL, we use a 'WHERE' clause with 'SELECT'. In Criteria the same Restriction can be achieved by the following way :


Criteria - Restrictions using WHERE

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.eq("name","Joe"));

'Criteria' class contains an 'add(...)' method, where we are specifying the actual restriction using 'Restrictions.eq("name","Joe")'.
Basically 'eq(...)' method stands for equal.


SQL query for the same would be:


SELECT * FROM EMPLOYEE WHERE NAME='Joe';

Criteria - Restrictions using WHERE - AND

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.and(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("city","Bangalore")
     ));

We have used an 'and(...)' method of 'Restrictions' and inside that we are using two 'eq(...)' method to check if the 'name' is equal to 'Joe' and 'city' is equal to 'Bangalore'.

SQL query for the same would be:


SELECT * FROM EMPLOYEE WHERE NAME='Joe' AND CITY='Bangalore';

Criteria - Restrictions using WHERE - OR

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.or(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("name","Peter")
     ));

We have used an 'or(...)' method of 'Restrictions' and inside that we are using two 'eq(...)' method to check if the 'name' is equal to 'Joe' or 'name' is equal to 'Peter'.

SQL query for the same would be:


SELECT * FROM EMPLOYEE WHERE NAME='Joe' OR NAME='Peter';

Adding Projection to Criteria

Projection is needed when you want to fetch partial columns from a database.


Example:

SELECT NAME FROM EMPLOYEE;

The equivalent code using 'Projection' would be :


Criteria criteria = session.createCriteria(Employee.class);
criteria.setProjection(Projections.property("name");

The above line is going to display all the values in the 'name' column.


Adding multiple columns in Projection

projectionList() method is needed to display multiple columns in 'Projection'.


Example :

SELECT NAME,CITY FROM EMPLOYEE;

The equivalent code using 'Projection' would be :


Criteria criteria = session.createCriteria(Employee.class);
criteria.setProjection(Projections.projectionList()
     .add(Projections.property("name"))
     .add(Projections.property("city"))
     );

Combining Restrictions and Projections

We can combine Restrictions and Projections and achieve something like below :


SELECT NAME,CITY FROM EMPLOYEE WHERE NAME='Joe' OR NAME='Peter';

The equivalent code using 'Projection' and 'Restriction' would be :


Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.or(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("name","Peter")
     ))
     .setProjection(Projections.projectionList()
     .add(Projections.property("name"))
     .add(Projections.property("city"))
     );

You can just add the '.setProjection(...)' method after the 'add(...)' method and you are done.


Example :

Assuming that we have an Employee class


Employee

class Employee {

   int id;
   String name;

   -- getters and setters --
}

And with the mapping files Employee.hbm.xml right in place, the main() class would be :


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

    Criteria criteria = session.createCriteria(Employee.class);

    // Below code is equivalent to - SELECT * FROM EMPLOYEE WHERE NAME='Joe'
    criteria.add(Restrictions.eq("name","Joe"));

    // Below code is equivalent to - SELECT * FROM EMPLOYEE WHERE NAME='Joe' AND CITY='Bangalore'
    criteria.add(Restrictions.and(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("city","Bangalore")
      ));

    // Below code is equivalent to - SELECT * FROM EMPLOYEE WHERE NAME='Joe' OR NAME='Peter'
    criteria.add(Restrictions.or(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("name","Peter")
     ));

    // Below code is equivalent to - SELECT NAME FROM EMPLOYEE
    criteria.setProjection(Projections.property("name");

    // Below code is equivalent to - SELECT NAME,CITY FROM EMPLOYEE
    criteria.setProjection(Projections.projectionList()
      .add(Projections.property("name"))
      .add(Projections.property("city"))
     );

    // Below code is equivalent to - SELECT NAME,CITY FROM EMPLOYEE WHERE NAME='Joe' OR NAME='Peter';
    criteria.add(Restrictions.or(
     Restrictions.eq("name","Joe"),
     Restrictions.eq("name","Peter")
     ))
      .setProjection(Projections.projectionList()
      .add(Projections.property("name"))
      .add(Projections.property("city"))
      );

    session.getTransaction().commit();

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