Criteria API- Hibernate Notes
To perform bulk select operation we use this criteria API.
Criteria API means we use criteria interface and we call methods of criteria interface for reading entities from the database.
Criteria API can be used only for select operation, we can not perform non select operation using criteria.
In criteria API , there will be less burden on programmer because query tuning
will be taken care by hibernate only.
To select entities from database using criteria API hen we need criteria object.
We can create criteria object by calling createCriteria method of session interface
ex: List list=session.createCriteria(Student.class);
Using criteria API we can read entities from database in following ways
1. If we want to read full entities form database by without applying any conditions then we need to directly call list method on criteria object.
List list=crit.list();
===============================================
2. If we want to read full entities with a condition ex deptno=20 then first we need to create criterion object for the condition and then we need to add critearion object in criteria.
Criteria crt=session.createCriteria(Student.class);
Criterion con= Restrictions.eq("age",20);
crt.add(con);
List list=crit.list();
Criteria iin an interface an Restriction is an class ,
In Restrictionclass all static method are exist , so we can call them directly by
class name.
Both Restriction class and Criterion interface are from same package it, org.hibernate.criterion
===============================================
If we want to read full entities with two conditions age=20 and sal greater then 40000 then we need to create a code like
Criteria crit=session.createcriteria(Employee.class);
Criterion on1=Restrictoins.eq("age","20");
Criterion on2=Restrictions.gt("sal",40000);
Criterion on3=restrictions.and(on1,on2);
crit.add(on3);
List list=crit.list();
==============================================
4. If we want to read partial entities then we need to create a Projection object for each property then we need to add projection object to projection List then we have to set projectlist object in criteria
Criteria crt=session.createCriteria(Student.class);
projection p1=Projection.Proeperty("name");
projection p2=Projection.Proeperty("sal");
ProjectionList pl=Projections.projectionList();
pl.add(p1);
pl.add(p2);
crit.setProjection(pl);
List list=crit.list()
Projection and ProjectionList are interface, projections is a class.
The above interface and classes from "org.hibernate.criterion"
=================================================
When we want to read a partial entity with a single property then we dont required a projection list we direclty set the projection object to criteria.
projection p1=Projection.Proeperty("name");
crti.setProjection(p);
============================================
While working with the Criteria Object , if if want to read a result by executing some agregate function then also we need take the support of projection
Criteria crit=session.createCriteria("Employee.class")
Projection p1=Projections.sum("emp sal")
Projection p2=Projections.max("employee sal")
Projection p3=Projections.rwoCount()
ProjectionList plist=Projetcions.projectionList();
plist.add(p1);
plist.add(p2);
plist.add(p3)
crit.setProjection(pList);
crit.list()
=======================================
Adding Sorting order to the Criteria:
Before executing a criteria we can add sorting order by creating order class object
if we add any sorting order internally hibernate reads data in sorting order and return objects in sorting order
Criteria crt=sessio.createCriteria(Employee.class)
Order o=Order.asc("sal")
crt.addOrder(o);
Pagination Methods:
Pagination means dividing a huge data into multiple pages
when developing pagination application the following two methods of criteria will be used to develop an application easily
1.setFirstresult - index of staring row
2.setMaxResult() -no of rows
Criteria crt=sessio.createCriteria(Employee.class);
crit.setFirstResult(2)
crit.setMaxResult(6)
The above criteria read 6 entities starting from 3.
Criteria API means we use criteria interface and we call methods of criteria interface for reading entities from the database.
Criteria API can be used only for select operation, we can not perform non select operation using criteria.
In criteria API , there will be less burden on programmer because query tuning
will be taken care by hibernate only.
To select entities from database using criteria API hen we need criteria object.
We can create criteria object by calling createCriteria method of session interface
ex: List list=session.createCriteria(Student.class);
Using criteria API we can read entities from database in following ways
1. If we want to read full entities form database by without applying any conditions then we need to directly call list method on criteria object.
List list=crit.list();
===============================================
2. If we want to read full entities with a condition ex deptno=20 then first we need to create criterion object for the condition and then we need to add critearion object in criteria.
Criteria crt=session.createCriteria(Student.class);
Criterion con= Restrictions.eq("age",20);
crt.add(con);
List list=crit.list();
Criteria iin an interface an Restriction is an class ,
In Restrictionclass all static method are exist , so we can call them directly by
class name.
Both Restriction class and Criterion interface are from same package it, org.hibernate.criterion
===============================================
If we want to read full entities with two conditions age=20 and sal greater then 40000 then we need to create a code like
Criteria crit=session.createcriteria(Employee.class);
Criterion on1=Restrictoins.eq("age","20");
Criterion on2=Restrictions.gt("sal",40000);
Criterion on3=restrictions.and(on1,on2);
crit.add(on3);
List list=crit.list();
==============================================
4. If we want to read partial entities then we need to create a Projection object for each property then we need to add projection object to projection List then we have to set projectlist object in criteria
Criteria crt=session.createCriteria(Student.class);
projection p1=Projection.Proeperty("name");
projection p2=Projection.Proeperty("sal");
ProjectionList pl=Projections.projectionList();
pl.add(p1);
pl.add(p2);
crit.setProjection(pl);
List list=crit.list()
Projection and ProjectionList are interface, projections is a class.
The above interface and classes from "org.hibernate.criterion"
=================================================
When we want to read a partial entity with a single property then we dont required a projection list we direclty set the projection object to criteria.
projection p1=Projection.Proeperty("name");
crti.setProjection(p);
============================================
While working with the Criteria Object , if if want to read a result by executing some agregate function then also we need take the support of projection
Criteria crit=session.createCriteria("Employee.class")
Projection p1=Projections.sum("emp sal")
Projection p2=Projections.max("employee sal")
Projection p3=Projections.rwoCount()
ProjectionList plist=Projetcions.projectionList();
plist.add(p1);
plist.add(p2);
plist.add(p3)
crit.setProjection(pList);
crit.list()
=======================================
Adding Sorting order to the Criteria:
Before executing a criteria we can add sorting order by creating order class object
if we add any sorting order internally hibernate reads data in sorting order and return objects in sorting order
Criteria crt=sessio.createCriteria(Employee.class)
Order o=Order.asc("sal")
crt.addOrder(o);
Pagination Methods:
Pagination means dividing a huge data into multiple pages
when developing pagination application the following two methods of criteria will be used to develop an application easily
1.setFirstresult - index of staring row
2.setMaxResult() -no of rows
Criteria crt=sessio.createCriteria(Employee.class);
crit.setFirstResult(2)
crit.setMaxResult(6)
The above criteria read 6 entities starting from 3.
thanks to make post sir.....
ReplyDeleteGreat post sir..
ReplyDelete