Tuesday, October 20, 2015

Criteria Query + Conditions To Criteria + criterion

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 cannot perform non-select operations using this criteria.

Criteria is suitable for executing dynamic queries too,

Friend lets take a flight of how to use this criteria queries in the hibernate
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

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

How to use criterion ?
Criteria criteria = session.createCriteria(Entity.class);
Criterion criterion =Restrictions.gt("id", new Integer(11));
// .gt() : means greater than
// id is a variable of our POJO class Entity.java
criteria.add(criterion); // adding criterion object to criteria class object
List l = criteria.list(); // executing criteria query
In our above example we are fetching the data by comparing id greater than (>) 11
If we want to put more conditions on the data (multiple conditions) then we can use
.and() method ,. or() method , .eq() method using the Restrictions class
How to use Restrictions?
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.and(Restrictions.like("name","%M%"),Restrictions.eq("category",ne
w String(“bird”))));
List list = criteria.list();
Iterator it = list.iterator();
Buddy, like this we can add required number of conditions

Conditions to Criteria
If we want to add some sorting order for the objects, before the objects are going to store
in list object then we need to add an Order class object to the Criteria class object by
calling addOrder() method

Order is a class given in “org.hibernate.Criterion” package

In Order class, we have 2 static methods, asc()[ascending order] and dsc()[descending
order] for getting an objects in required order

Internal concept is, hibernate will select the records (rows) from ENTITIES_DATA table
and stores them into a ResultSet and then converts each row data of resultset into a POJO
class object basing on our field type, then add all these objects into a list according to the
order we have given

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...