Share on Google+Share on Google+

Hibernate Criteria Query Example

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

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
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 = 
new Configuration().configure()
  session = 
  //Criteria Query Example
  Criteria crit = 
  List insurances = 
  for(Iterator it = 
  Insurance insurance = 
ID: " 
+ insurance.getLngInsuranceId());
Name: " 
+ insurance.getInsuranceName());
  catch (Exception e) {
  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.


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

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Criteria Query Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 7, 2011

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

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

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