Get all updates of Java4s
Follow Us: Email 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:

 

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:

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:

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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. 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.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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