Newsletter |
Criteria Query, Hibernate Criteria Query Introduction
Unlike HQL, Criteria is only for selecting the data from the database, that to 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 see this angle later, 😉 but for now see how we are using criteria for selecting complete objects form the database. We cant perform non-select operations using this criteria. Criteria is suitable for executing dynamic queries too, let us see how to use this criteria queries in the hibernate..
syntax:
Criteria crit = session.createCriteria(–Our class object–);
Usage:
Criteria crit = session.createCriteria(Employee.class); // let Product is our pojo class List l = crit.list() // need to call list() to execute criteria Iterator it = l.iterator(); while(it.hasNext()) { Object o = it.next(); Product p = (Product)o; ------ ----- ----- }
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
crit.add(Criterion Interface Object)
crit = 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
Usage:
Criteria crit = session.createCriteria(Products.class); Criterion c1=Restrictions.gt("price", new Integer(12000)); //price is our pojo class variable crit.add(c1); // adding criterion object to criteria class object List l = crit.list(); // executing criteria query
Note: See line number 2, am calling gt(-,-) method of Restrictions class, (means greater than), in our above example am fetching the data by comparing price greater than (>) 12000
- If we want to put more conditions on the data (multiple conditions) then we can use and method , or method give by the Restrictions class
Usage:
crit.add(Restrictions.and(Restrictions.like("proName","%R%"), Restrictions.eq("price",new Integer(12000)))); List l=crit.list(); Iterator it = l.iterator();
Like this we can add any number of conditions…
Let us see an example program on HQL criteria in the next session…
You Might Also Like
::. About the Author .:: | ||
hi java4s the site provided hibernate examples are very nice and they are very easy to understand..please re modify these examples with annotation support..
thank you
kalyani
HI
I need examples like to join more than one tables and also the update the records using criteria, pls give the examples related to this
Exact meaning of n+1 problem in hibernate?
how to integrity hibernate-spring in simple way?
Examples are good and simple please provide complex examples on HQL
Hi Bro
Nice Tutorial
Could you please upload fetching types and fetching modes in hibernate
you have loaded the Employee class and retrieving the product element this is not fair we believe you and u r doing Error its not fair
Hi,
Please post diffrence between lazy loading and early loading in hibernate?
when use lazy loading and when use use early loading in hibernate?
Check the similar concept..
https://www.java4s.com/hibernate/difference-between-hibernate-get-and-load-methods/