Thursday, 27 December 2012

Difference between Hibernate createCriteria, createQuery, createSQLQuery

To create query in the Hibernate ORM framework, there is three different types. The following are the three ways to create query instance:

  1. session.createQuery()
  2. session.createSQLQuery()
  3. session.createCriteria()

session.createQuery()
The method createQuery() creates Query object using the HQL syntax.
Query query = session.createQuery("from Student s where s.name like 'k%'");

session.createSQLQuery()
The method createSQLQuery() creates Query object using the native SQL syntax.
Query query = session.createQuery("Select * from Student");

session.createCriteria()
The method createCriteria() creates Criteria object for setting the query parameters. This is more useful feature for those who don't want to write the query in hand.
Criteria criteria = session.createCriteria(Student.class);

No comments:

Post a Comment