anoop
anoop

Reputation: 1614

Difference between Hibernate createCriteria, createQuery, createSQLQuery functions

Can anyone please tell me the difference between Hibernate's:

Can anyone tell me what data these three functions return, c.q. direct me to a proper and simple link to study these Hibernate functions?

Upvotes: 31

Views: 45698

Answers (4)

Ravi Parekh
Ravi Parekh

Reputation: 5622

------------------------
        PERSON
------------------------
**DB_Column**| **POJO**
PERSON_ID    | personID
------------------------

createQuery()

you are using pojo fields. Using HQL syntax.

Query query = session.createQuery("from Person s where s.personID like 'A%'");

//    returns: 

List<Person> persons = query.list();

createSQLQuery()

You are using Native|DB fields. After googling some site, Came to know this will also clear the cache as hibernate don't know the what you have executed.

Query query = session.createSQLQuery("select s.* from Person s where s.person_ID like 'A%'");

//    returns: 

List<Object[]> persons = query.list();.

createCriteria()

  • Create sql query using Criteria object for setting the query parameters.
  • Useful when switching DB.
  • Read only query

    Criteria criteria = session.createCriteria(Person.class);
    criteria.add(Restrictions.like("personId", "A%"));
    List<Person> persons = criteria .list();
    

Upvotes: 5

Mohammadreza Khatami
Mohammadreza Khatami

Reputation: 1332

createSQLQuery -- is for native sql querying which is selected by you with jdbc driver cfg or something else.

createQuery -- is for hibernate querying which provides you independent querying which makes you run that on many databases using API and more other advantages.

createCriteria -- is better to use for simple querying on db because of it's simplicity.

I hope this helps you!

Upvotes: 0

subhashis
subhashis

Reputation: 4888

1. session.createQuery()-> Can create query using HQL and can perform CRUD Operations 

Example:

      Query query = session.createQuery("from Student");
      List list=quey.list();

      Query query = session.createQuery("update Student where studentid=9");
      int result=query.executeUpdate();

      Query query = session.createQuery("delete Student where studentid="+ studentId);
      int result=query.executeUpdate();

      Query query = session.createQuery("insert into Student where studentid="+ studentId);
      int result=query.executeUpdate();
  1. session.createSQLQuery()-> Can create query using SQL and can perform CRUD Operations
  2. session.createCriteria()->Can create query using Criteria API and can perform only Read Operations

Upvotes: 9

Ramesh Kotha
Ramesh Kotha

Reputation: 8322

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

Look into the details of each category in detail.

Session.createQuery()

The method createQuery() creates Query object using the HQL syntax. For 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. For 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);

Upvotes: 45

Related Questions