Criteria Query Introduction



Unlike HQL, Criteria Query is only for selecting the data from the database
Using Criteria query we can select complete objects only not partial objects
In fact by combining criteria and projections concept we can select partial objects too we will learn this later.
We cannot perform non-select operations using this criteria.
Criteria is suitable for executing dynamic queries too.

Syntax:

Criteria criteria = session.createCriteria(“Class Name”.class);

How to use ?
Criteria criteria = session.createCriteria(Entity.class);
List list = criteria.list()
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
    Object obj = iterator.next();
    Entity entity = (Entity)obj;
    /*------ Further code ----- */

}

Adding Conditions To Criteria
If we want to put conditions to load data from database, using criteria then we need to create one Criterion Interface object and we need to add this object to Criteria Class object.

Criterion is an interface given in “org.hibernate.criterion” package
In order to get Criterion object, we need to use Restrictions class.

Restrictions is the factory for producing Criterion objects, but friends there is no explicit relation between Criterion interface and Restrictions class, it means Restrictions class is not implemented from Criterion Interface.

In Restrictions class, we have all static methods and each method of this class returns Criterion object


Restrictions class is also given in “org.hibernate.criterion” package