Home Hibernate Hibernate CreateCriteria



Hibernate CreateCriteria
Posted on: July 28, 2012 at 12:00 AM
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();
System.out.println("RollNo.\tName\tCourse");
while (iterator.hasNext()) {
Student student = (Student) iterator.next();
System.out.print(student.getRoll());
System.out.print("\t"+student.getName());
System.out.println("\t"+student.getCourse());
}
session.flush();
} catch (HibernateException e) {

e.printStackTrace();
} finally {
session.close();
}
}
}

Output :

Hibernate: select this_.roll_no as roll1_0_0_, this_.course as course0_0_, this_.name 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

Related Tags for Hibernate CreateCriteria:


More Tutorials from this section

Ask Questions?    Discuss: Hibernate CreateCriteria  

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.