Using a Named query
A named query, also called a static query, is defined with the entity class. When the query is deployed, JPA can translate the query into native SQL resulting in an improvement in the query's performance.
The process of creating and using a named query has three steps:
- Declaring the named query using the @NamedQuery annotation as part of an entity class
- Using the
createNamedQuerymethod to create the query
- Executing the query
A named query is declared as part of the entity class. In its simplest form, the @NamedQuery annotation precedes the class declaration and uses two attributes. The first attribute,
name, sets the name of the query and the second attribute,
query, contains the query. The query itself can be any ...