@NamedQuery over @NamedNativeQuery

Abhi picture Abhi · Dec 19, 2012 · Viewed 20.9k times · Source

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.

Answer

Oleksi picture Oleksi · Dec 19, 2012

@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.