Newsletter

Criteria Query, Hibernate Criteria Query Introduction

Hibernate » on Jul 12, 2011 { 8 Comments } By Sivateja

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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

8 Responses to “Criteria Query, Hibernate Criteria Query Introduction”
  1. kalyani says:

    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

  2. Issak Madeena says:

    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

  3. HARI KISHORE says:

    Exact meaning of n+1 problem in hibernate?
    how to integrity hibernate-spring in simple way?

  4. poojith says:

    Examples are good and simple please provide complex examples on HQL

  5. ram says:

    Hi Bro

    Nice Tutorial

    Could you please upload fetching types and fetching modes in hibernate

  6. vishal says:

    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

  7. Mahi says:

    Hi,

    Please post diffrence between lazy loading and early loading in hibernate?
    when use lazy loading and when use use early loading in hibernate?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.