Annotation Interface NamedQuery


@Repeatable(NamedQueries.class) @Target(TYPE) @Retention(RUNTIME) public @interface NamedQuery
Declares a named query written in the Jakarta Persistence query language. Query names are scoped to the persistence unit. A named query may be executed by calling EntityManager.createNamedQuery(String, Class).

The following is an example of the definition of a named query written in the Jakarta Persistence query language:

@NamedQuery(
    name = "findAllCustomersWithName",
    query = "SELECT c FROM Customer c WHERE c.name LIKE :custName")

The named query may be executed like this:

@PersistenceContext EntityManager em;
...
List<Customer> customers = em.createNamedQuery("findAllCustomersWithName", Customer.class)
              .setParameter("custName", "Smith")
              .getResultList();
The NamedQuery annotation can be applied to an entity class or mapped superclass.
Since:
1.0