DEV Community

realNameHidden
realNameHidden

Posted on

JPA Criteria API | Hibernate — Criteria Queries

Criteria API

->it is the Object based persistence logic where no queries will be written the
entire persistence logic will be developed by using the java statements

->this persistence 100% java statements based persistence , so it
is database s/w independent persistence logic

-> while developing these persistence logic we use class names and property names

For Detailed example on using criteria api you can watch video:
https://youtu.be/n4Ox43N3N4o

JPA Criteria API

->Modern API — given by JPA and supported by hibernate
->Supports both select and non-select queries
->Support both entity select operation i.e all col values and
Scalar operations(Specific col values)

=>Main Object or Base Object in this api is CriteriaBuilder Object
and this Object is used to create CriteriaQuery Obj,Root Obj,Parameter Obj,
Order Objs, Predicate Obj,CriteriaUpdate and CriteriaDelete Obj

To create the Criteria Query Follow the below steps

1)Create the Object of CriteriaBuilder interface
Session ses = HiebernateUtil.getSession();
CriteriaBuilder cb = ses.getCriteriaBuilder();

2)Create the CriteriaQuery Object
CriteriaQuery cq = cb.createQuery(Entity.class);

3)Create the root Object Specifying the from operation
Root root = cq.from(EntityClassName.class);

4)apply the clause condition if any
cq.select(root).where(cb.ge(root.get(“id”),5),cb.le(root.get(“id”),10)));

5)Create the query Object having the CriteriaQuery Object
Query query = ses.createQuery(cq);

6)Now,Control the execution of query by calling the methods of query Interface
List list = query.getResultList();

Top comments (0)