Hibernate Criteria Query Example

The Criteria interface allows to create and execute object-oriented queries.


 

Ads

Tutorials   
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 Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial
Ads

Hibernate Criteria Query Example

     

The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective. 

The interface org.hibernate.Criteria is used to create the criterion for the search. The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances. Here is a simple example of Hibernate Criterial Query:


 
package roseindia.tutorial.hibernate;

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

 *  
 */public class HibernateCriteriaQueryExample {
  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);
  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 {
  }  
  }
}

The above Criteria Query example selects all the records from the table and displays on the console. In the above code the following code creates a new Criteria instance, for the class Insurance:

Criteria crit = session.createCriteria(Insurance.class);

The code:

List insurances = crit.list();

creates the sql query and execute against database to retrieve the data.

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Criteria Query Example

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

Advertisements

Ads
 

Discuss: Hibernate Criteria Query Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
papireddy
April 7, 2011
hibernate

in hibernate having 1)criteri API 2)nagivi sql and 3)hql query i want differences with examples Plz send me.
siraj
June 26, 2011
need faq of hibernate

it is good and simple please send to my id all faq's
ram
September 1, 2011
hibernate

how to retrieve the values from morethan one table in hibernate?

Ads

 

Ads