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.

Getting ready

The process of creating and using a named query has three steps:

  1. Declaring the named query using the @NamedQuery annotation as part of an entity class
  2. Using the createNamedQuery method to create the query
  3. 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 ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.