Criteria
API in Hibernate
Till now we’ve two ways of saving and retrieving objects in
hibernate
1) get(),save()
2) using HQL ,
query object
Both above approaches require knowledge of SQL , Criteria API
in hibernate doesn’t require any prior knowledge of SQL
public class HibernateTestCritereiaAPI {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session
session = sessionFactory.openSession();
session.beginTransaction();
// Criteria is alike where condition
in HQL/SQL
Criteria
criteria = session.createCriteria(Person.class);
// Restrictions are where clauses
and by default it acts as &
criteria.add(Restrictions.eq("id",6L));
List<Person>
list = (List<Person>) criteria.list();
for(Person p: list)
System.out.println(p.getName());
session.getTransaction().commit();
session.close();
}
}
Output:
Hibernate: select this_.id as id0_0_, this_.name as
name0_0_ from PERSON this_ where this_.id=?
Person 6
No comments:
Post a Comment