Share on Google+Share on Google+

Hibernate CreateCriteria

In this section we will discuss how to create criteria with example.

Hibernate CreateCriteria

In this section we will discuss how to create criteria with example.

Hibernate CreateCriteria()  :

Criteria is highly object oriented concept of Hibernate. It is used to create and execute object-oriented queries.
As HQL is minimal Object oriented ,so you can say it is good alternative of HQL.
For Criteria interface import org.hibernate.Criteria. Session works as factory for the criteria instance.
It is very useful where number of conditions are used. CreateCriteria() method is used to call Criteria query.

Criteria criteria=session.createCriteria(Student.class);

Example :

package net.roseindia.main;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria=session.createCriteria(Student.class);
try {
List<Student> list =new ArrayList<Student>(); 
list= criteria.list();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student);
} catch (HibernateException e) {

} finally {

Output :

Hibernate: select this_.roll_no as roll1_0_0_, this_.course as course0_0_, as name0_0_ from student this_
RollNo.   Name    Course
1         Ron     java
3         Roxi    unix
4         Jenson  Hibernate
5         jacqub  Hibernate
6         Mandy   C
8         John    Hibernate
9         Linda   Hibernate

Click here to download complete source code



Posted on: July 28, 2012 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 CreateCriteria  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image