Is there any benefit of using @NamedQuery
over @NamedNativeQuery
in hibernate or vice verse. I cant spot the exact difference or in which context we should use @NamedQuery
over @NamedNativeQuery
Thanks in advance.
In some cases it can happen Hibernate/JPA does not generate the most efficient statements, so then native SQL can be faster - but with native SQL your application loses the portability from one database to another, so normally is better to tune the Hibernate/JPA Query mapping and the HQL statement to generate more ...
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. Similar to how the constant is defined. NamedQuery is the way you define your query by giving it a name.
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.
There are three basic types of JPA Queries: Query, written in Java Persistence Query Language (JPQL) syntax. NativeQuery, written in plain SQL syntax. Criteria API Query, constructed programmatically via different methods.
@NamedNativeQuery
lets you write a named SQL query, while @NamedQuery
lets you write a named HQL query (or JPQL).
In general, you should prefer to write HQL queries because then you can let Hibernate handle the intricacies of converting the HQL into the various SQL dialects. This will make your job much simpler when you choose to switch DBMS providers.
When taking about performance, you have to know something about what goes on under the hood.
You have probably programmed something using straightforward JDBC, so you know how to queries get passed to the driver and send to the database. When using HQL or JPA-QL the queries first have to be parsed into an SQL language that the database can understand. In this case, we have an extra parsing step in between. Note that Native SQL queries, including stored procedure calls, the persistence framework still takes care of mapping the JDBC result sets to graphs of persistent objects.
If you want to include a native SQL hint to instruct the database management systems query optimizer, for example, you need to write the SQL yourself. HQL and JPA-QL do not have keywords for this.
The disadvantage of putting native SQL in your mapping metadata is lost database portability, because your mappings, and hence your application, will work only for a particular database. But usually this is of a minor concern as you are probably not creating a framework that has to work on every database.
When you want to get behind the performance of your query, you really have to consult the database and look at the execution plan - A DBA can tell you exactly what is good and what can be optimized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With