4

Are sql queries specified inside @NamedNativeQuery pre-compiled just like @NamedQuery in JPA? I am asking this because I couldn't find anything stating it is or it isn't.

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
Prateek Jain
  • 2,738
  • 4
  • 28
  • 42
  • this is not something directly to the question but it might help the perspective https://stackoverflow.com/questions/27565706/are-hibernate-named-queries-precompiled-in-the-true-sense/27574444 – Shafin Mahmud Sep 19 '18 at 09:39

2 Answers2

3

@NamedQuery "pre-compilation" is basically translating in advance to the native query language (typically SQL), so you can do it just once at application start / first use and not every time you issue the query.

@NamedNativeQuery queries are written in the native query language already, so in this sense, they are "intrinsically" precompiled.

Phenomenal One
  • 2,501
  • 4
  • 19
  • 29
gpeche
  • 21,974
  • 5
  • 38
  • 51
  • "queries are written in the native query language already": It is not necessary true. For example http://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#sql-alias-references Hibernate provides such kind of alias reference which require extra translation based on entity definition. (However I don't really know if it is going to do precompilation for `@NamedNativeQuery` though :P ) – Adrian Shum Sep 19 '18 at 09:50
  • 1
    @Adrian Shum Hibernate native queries != JPA native queries – gpeche Sep 19 '18 at 09:55
0

The amount of pre-processing done to the queries annotated with @NamedNativeQuery is dependent on the JPA Provider, however you shouldn't assume much is happening, since the query is native to the underlying database, so nothing is happening at the JPA level. This is especially the case if you are calling stored procedures or something very database specific JPA is not aware of. There is no translation from JPQL to SQL.

What might go on underneath the hood is some optimisations around prepared statements for those named queries. But it depends on the JPA provider and its level of interaction with the JDBC driver of your specific database.

jbx
  • 21,365
  • 18
  • 90
  • 144