Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Native Query vs Named Query

 
amol skulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it discouraged to use the native query in Hibernate . What are the disadvantages of using native query. In which case do we need to use native query.
How is caching performance affected by native query. What are advantages from performance perspective of Named Query over native query.
 
Aby Krishna
Greenhorn
Posts: 14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
General understanding is that a Named Query (written in HQL or JPQL) is going to be optimized and converted to native sql dialect by the underlying persistence framework. A native query is something written in SQL dialect by you, over which the underlying persistence framework may not have control and hence may not be able to optimise or convert.

Further, from the hibernate spec:

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. Note that Hibernate allows you to specify handwritten SQL (including stored procedures) for all create, update, delete, and load operations (please refer to the reference guide for more information.)
 
amol skulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about Caching of native query
 
Aby Krishna
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't be cached unless you use the addScalar() or addEntity() methods.

Look: https://forum.hibernate.org/viewtopic.php?p=2373167
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic