Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Hibernate createCriteria, createQuery, createSQLQuery functions

Tags:

hibernate

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

  • createCriteria
  • createQuery
  • createSQLQuery

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?

like image 855
anoop Avatar asked Dec 26 '11 14:12

anoop


People also ask

What is CreateQuery in hibernate?

CreateQuery: Used to create an HQL. createNamedQuery: Used to define queries with name in mapping file or annotation. See this. createNativeQuery: Used to execute native/pure SQL queries. Example.

What is the difference between query and criteria in hibernate?

HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries. HQL is to perform both select and non-select operations on the data, Criteria is only for selecting the data, we cannot perform non-select operations using criteria.

What is createSQLQuery?

Hibernate Native SQL Example For Hibernate Native SQL Query, we use Session. createSQLQuery(String query) to create the SQLQuery object and execute it. For example, if you want to read all the records from Employee table, we can do it through below code.

What are the query types used in hibernate?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.


2 Answers

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); 
like image 99
Ramesh Kotha Avatar answered Sep 17 '22 14:09

Ramesh Kotha


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
like image 39
subhashis Avatar answered Sep 16 '22 14:09

subhashis