Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to create native query?

Tags:

java

orm

jpa

I am working in a project which uses JPA ORM and framework provides two kinds of method to create queries.

  • entityManager.createQuery(query1);
  • entityManager.createNativeQuery(query2);

I understand the kinds of query string is to be passed to use them, but I don't know exactly why do we need to create native query? Probably we don't want to use ORM capabilities there?

like image 296
Vardan Gupta Avatar asked Nov 08 '12 11:11

Vardan Gupta


People also ask

Why do we use native query?

You may also express queries in the native SQL dialect of your database. This is useful if you want to utilize database specific features such as query hints or the CONNECT BY option in Oracle. It also provides a clean migration path from a direct SQL/JDBC based application to Hibernate.

What is create native query?

Create ad-hoc native queries Creating an ad-hoc native query is quite simple. The EntityManager interface provides the createNativeQuery method for it. It returns an implementation of the Query interface, which is the same that you get when you call the createQuery method to create a JPQL query.

What is the use of native query in JPA?

Native query refers to actual sql queries (referring to actual database objects). These queries are the sql statements which can be directly executed in database using a database client. 2. Named query is the way you define your query by giving it a name.

What is the use of native query in Hibernate?

Hibernate Native SQL Entity and Join *} is used to return all properties of an entity. When we use addEntity() and addJoin() with join queries like above it returns both the objects, as shown above. Output produced by above code is like below.


2 Answers

You do not need to create a native query unless you want to. JPQL eventually is translated into SQL by the framework but the framework lets you call the native query also. Why would want to do that:

  • Low level access, which means that you can optimize and handle the mapping by yourself; with SQL you actually access the database table while with JPQL you access the entity objects;
  • Maybe you do not want to learn JPQL if you already know SQL
  • You already have the queries written in SQL, and do not have resources/time to port them to JPQL
like image 166
Random42 Avatar answered Oct 04 '22 06:10

Random42


createQuery uses JPAs own query language, you select from Class names instead of table names. This is not SQL, it is just similar, and is later transformed to real SQL. Mapping to java classes will be done automatically and actual class instances will be returned as result.

createNativeQuery uses real SQL, and will not be able to use JPA features. This method is used in general if you need to do something really odd that is not supported by JPA. A list of Object[] will be returned, and mapping to java objects will have to be done manually. In other words, its just like working with a DB before JPA came to, just slightly more convenient since connection handling is done automatically.

like image 30
Rasmus Franke Avatar answered Oct 04 '22 05:10

Rasmus Franke