Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create query in Hibernate

Tags:

java

hibernate

When we have to use

createQuery(String),   

createNamedQuery(String),  

createNativeQuery(String)   

in Hibernate and what is the difference between them?

like image 589
Mahendra Athneria Avatar asked Jul 13 '11 12:07

Mahendra Athneria


People also ask

What is create query 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.

Can we write SQL query in Hibernate?

You can use native SQL to express database queries if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. Hibernate 3. x allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.

Can you explain query 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

  1. CreateQuery: Used to create an HQL.

  2. createNamedQuery: Used to define queries with name in mapping file or annotation. See this.

  3. createNativeQuery: Used to execute native/pure SQL queries. Example

like image 106
Nikunj Avatar answered Oct 11 '22 19:10

Nikunj


They differ in the meaning of the argument they are called with.

  • createQuery takes an actual JP-QL query as argument.
  • createNamedQuery takes the name of a query as argument, which is defined elsewhere, e.g. with a @javax.persistence.NamedQuery annotation.
  • createNativeQuery is called with a SQL query.
like image 40
phlogratos Avatar answered Oct 11 '22 18:10

phlogratos