Criteria is used to get complete data from database and return complete object ,It is used to apply condition in object using Restriction class and Criterion Interface.

It is only for Data Selection  not for Data Insertion ,Updation and Deletion.

Code of  Data Selection :-

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import scs.Student;

public class CriteriaExample {

public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session s =sf.openSession();
        Criteria crt = s.createCriteria(Student.class);
       // Criterion crt1 = Restrictions.gt("fees", 12000);
        Criterion crt1 = Restrictions.like("sname", "%m%");
        crt.add(crt1);
        List lst = crt.list();
        Iterator it = lst.iterator();
        while(it.hasNext())
        {
        Student stu = (Student)it.next();
        System.out.println(stu.getRno() +" "+stu.getSname()+" "+stu.getBranch()+" "+stu.getFees());
        }
       
       s.close();
}

}






Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post