Home Hibernate Hibernate CreateCriteria

 
 

Share on Google+Share on Google+

Hibernate CreateCriteria

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: July 28, 2012

Related Tutorials

Discuss: Hibernate CreateCriteria  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com