Three ways to create query in Hibernate

Create Query in Hibernate

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()

We will look into the details of each category in detail.

also read:

session.createQuery()

The method createQuery() creates Query object using the HQL syntax. Fro example

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. Fro example

Query query = session.createSQLQuery("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. You can specify any type of complicated syntax using the Criteria API.

Criteria criteria = session.createCriteria(Student.class);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!