Mahendra Athneria
Mahendra Athneria

Reputation: 1213

create query in Hibernate

When we have to use

createQuery(String),   

createNamedQuery(String),  

createNativeQuery(String)   

in Hibernate and what is the difference between them?

Upvotes: 14

Views: 41261

Answers (3)

phlogratos
phlogratos

Reputation: 13924

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.

Upvotes: 4

Nikunj
Nikunj

Reputation: 3128

  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

Upvotes: 17

Related Questions