Criteria Query Examples

In the last lesson we learnt how to use Criteria Query to select all the records from Insurance table.

Ads

Tutorials   
Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment
Ads

Criteria Query Examples

     

In the last lesson we learnt how to use Criteria Query to select all the records from Insurance table. In this lesson we will learn how to restrict the results returned from the database. Different method provided by Criteria interface can be used with the help of Restrictions to restrict the records fetched from database.


 

 

 

Criteria Interface provides the following methods:

Method Description
add The Add method adds a Criterion to constrain the results to be retrieved.
addOrder Add an Order to the result set.
createAlias Join an association, assigning an alias to the joined entity
createCriteria This method is used to create a new Criteria, "rooted" at the associated entity.
setFetchSize This method is used to set a fetch size for the underlying JDBC query.
setFirstResult This method is used to set the first result to be retrieved.
setMaxResults This method is used to set a limit upon the number of objects to be retrieved.
uniqueResult
  
This method is used to instruct the Hibernate to fetch and return the unique records from database.

Class Restriction provides built-in criterion via static factory methods. Important methods of the Restriction class are:

Method Description
Restriction.allEq
  
This is used to apply an "equals" constraint to each property in the key set of a Map
Restriction.between
  
This is used to apply a "between" constraint to the named property
Restriction.eq
  
This is used to apply an "equal" constraint to the named property
Restriction.ge
  
This is used to apply a "greater than or equal" constraint to the named property
Restriction.gt
  
This is used to apply a "greater than" constraint to the named property
Restriction.idEq This is used to apply an "equal" constraint to the identifier property
Restriction.ilike
  
This is case-insensitive "like", similar to Postgres ilike operator
Restriction.in This is used to apply an "in" constraint to the named property
Restriction.isNotNull This is used to apply an "is not null" constraint to the named property
Restriction.isNull   This is used to apply an "is null" constraint to the named property
Restriction.le  This is used to apply a "less than or equal" constraint to the named property
Restriction.like This is used to apply a "like" constraint to the named property
Restriction.lt This is used to apply a "less than" constraint to the named property
Restriction.ltProperty This is used to apply a "less than" constraint to two properties
Restriction.ne  This is used to apply a "not equal" constraint to the named property
Restriction.neProperty This is used to apply a "not equal" constraint to two properties
Restriction.not   This returns the negation of an expression
Restriction.or  This returns the disjuction of two expressions

Here is an example code that shows how to use Restrictions.like method and restrict the maximum rows returned by query by setting the Criteria.setMaxResults() value to 5.

package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.criterion.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net 
Hibernate Criteria Query Example

 *  
 */public class HibernateCriteriaQueryExample2 {
  public static void main(String[] args) {
  Session session = null;
  try {
  // This step will read 
hibernate.cfg.xml and prepare hibernate for

  // use
  SessionFactory sessionFactory
 = 
new Configuration().configure()
  .buildSessionFactory();
  session = 
sessionFactory.openSession
();
  //Criteria Query Example
  Criteria crit = 
session.createCriteria
(Insurance.class);
  crit.add(Restrictions.like("
insuranceName"
"%a%"))//Like condition
  crit.setMaxResults(5)//
Restricts the max rows to 5


  List insurances = crit.list();
  for(Iterator it = 
insurances.iterator
();it.hasNext();){
  Insurance insurance =
 
(Insuranceit.next();
  System.out.println("
ID: " 
+ insurance.getLngInsuranceId());
  System.out.println("
Name: " 
+ insurance.getInsuranceName());
  
  }
  session.close();
  catch (Exception e) {
  System.out.println(e.getMessage());
  finally {
  }  
  }
}

Advertisements

Ads
Share on Google+Share on Google+

Criteria Query Examples

Posted on: March 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Criteria Query Examples   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Shiva
November 14, 2012
Thanks

Thanks RoseIndia for this Tutorial.

Ads

 

Ads