Criterion API

Criterion API

By using Session interface methods like save(), persist(), update(), delete(), etc. we are able to perform single record manipulation, but, if we want to perform manipulations over multiple records then we must go for HQL, Native SQL and Criterion API.

Where HQL is a powerful, Object Oriented and Database Independent Query language provided by Hibernate, but, HQL is not providing environment to perform DDL kind of operations and it is not supporting database dependent native operations like invoking stored procedures and functions, etc.

To overcome the above problem with HQL we will use “Native SQL”, in case of Native SQL we have to write database dependent SQL queries directly, but, it is against to Hibernate, as per the Hibernate view we must not write database dependent SQL queries in JAVA applications.

In Hibernate applications, to avoid totally query languages like SQL and HQL, and to provide the completed Persistence logic in the form of JAVA code we must use “Criterion API”.

In the case of Criterion API, we will define persistence logic by using JAVA code only, where the required predefined library was provided by Hibernate in the form of “org.hibernate” package and “org.hibernate. criterion” package.

If we want to use Criterion API in Hibernate applications then we have to use the following steps.

1. Create Criteria Object

Criteria object is a central object in Criteria API, it able to manage HQL query representation internally and it has provided predefined methods to defined query logic. To create Criteria object we have to use the following method from Session.

public Criteria createCriteria(Class cls);

E.g

Criteria c = session.createCriteria(com.ashok.hibernate.model.Employee.class);

Note

It is equivalent to the HQL query internally “from Employee”.

2. Prepare Criterion objects and add that Criterion objects to Criteria object

Criterion is an object , it able to manage a single Conditional expression in database logic. To create Criterion object we have to use the following methods from “org.hibernate.Restrictions” class.

public static Criterion isEmpty(String property)
public static Criterion isNotEmpty(String property)
public static Criterion isNull(String property)
public static Criterion isNotNull(String property)
public static Criterion in(String property, Object[] obj)
public static Criterion in(String property, Collection c)
public static Criterion between(String property, Object min_Val, Object max_Val)
public static Criterion between(String property, Object[] obj)
public static Criterion eq(String property, Object val)
public static Criterion ne(String property, Object val)
public static Criterion lt(String property, Object val)
public static Criterion le(String property, Object val)
public static Criterion gt(String property, Object val )
public static Criterion ge(String property, Object val)

To add a particular Criterion object to Criteria object we have to use the following method.

public void add(Criterion c)

E.g

Criterion c1 = Restrictions.ge("esal", 60000);
Criterion c2 = Restrictions.le("esal", 90000);
c.add(c1);
c.add(c2);

Note

With the above steps, Criteria object is able to prepare the query like “from Employee esal>=6000 and esal<=9000”.

3. Create Projection objects , add Projection objects to ProjectionList and add ProjectionList to Criteria object

The main intention of Projection object is to represent a single POJO class property.

To get Projection object with a particular Property name we have to use the following method from “Projections” class.

public static Projection projection(String proName)

To create ProjectionList object we have to use the following method from Projections class.

public static ProjectionList projectionList()

To add Projection object to ProjectionList we have to use the following method from ProjectionList class.

public void add(Projection p)

To set ProjectionList to Criteria object we have to use the following method.

public void setProjection(ProjectionList pl)

E.g

ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("empid"));
pl.add(Projections.property("empname"));
pl.add(Projections.property("salary"));
pl.add(Projections.property("address"));
c.setProjection(pl);

With the above , Criteria object is able to prepare HQL query internally like below.

E.g

select eno, ename, esal, eaddr from Employee where esal >=6000 and esal<=90000
4. Provide a particular Order to the query

To represent a particular Order over the results, we have to use either asc(-) or desc(-) methods from “Order” class.

public static Order asc(String propName)
public static Order desc(String propName)

To add Order object to Criteria object we have to use the following method.

public void addOrder(Order o)

E.g

Order o = Order.desc("empname");
c.addOrder(o);

Note

With this, Criteria object is able to create HQL query like

select empid, empname, salary, address from Employee where salary >=6000 and salary<=90000 order by desc(empname)
5. Execute Database logic which we provided in Criteria object

To execute database logic existed in Criteria object we have to use the following methods.

public List list()
public Iterator iterate()
public ScrollableResults scroll()
public Object uniqueResult()

E.g

List<Object[]> list = c.list();

Note

Like HQL, we are able to provide custom properties over the Criteria object by using the following methods.

public void setFetchSize(int size)
public void setFirstResult(int position)
public void setMaxResults(int val)
public void readOnly(boolean b)
pbulic void cacheable(boolean b)
Criterion API
Scroll to top