Annotation Interface NamedQuery
SELECT query.
This annotation may be applied to any class or interface belonging to the persistence unit.
@NamedQuery(name = "findAllCustomersWithName",
query = "SELECT c FROM Customer c WHERE c.name LIKE :custName")
@Entity
class Customer { ... }
A named query may be executed by calling
EntityHandler.createNamedQuery(String, Class).
List<Customer> customers =
em.createNamedQuery("findAllCustomersWithName", Customer.class)
.setParameter("custName", "Smith")
.getResultList();
Alternatively, a reference to a named query may be obtained via the static metamodel.
List<Customer> customers =
em.createQuery(Customer_._findAllCustomersWithName_)
.setParameter("custName", "Smith")
.getResultList();
- Since:
- 1.0
- See Also:
- API note:
- A named
UPDATEorDELETEstatement is usually declared usingNamedStatement. For backward compatibility, this annotation may be used instead, but this usage is no longer encouraged.
-
Required Element Summary
Required ElementsModifier and TypeRequired ElementDescription(Required) The name used to identify the query in calls toEntityHandler.createNamedQuery(String).(Required) The query string in the Jakarta Persistence query language. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescription(Optional) The name of a named entity graph interpreted as a load graph and applied to the entity returned by the query.(Optional) Query properties and hints.(Optional) The lock mode type to use in query execution.(Optional) The pessimistic lock scope to use in query execution if a pessimistic lock mode is specified vialockMode().Class<?> (Optional) The class of each query result.
-
Element Details
-
name
String name(Required) The name used to identify the query in calls toEntityHandler.createNamedQuery(String). -
query
String query(Required) The query string in the Jakarta Persistence query language. -
resultClass
Class<?> resultClass(Optional) The class of each query result.When the result class is explicitly specified, either:
- the select list of the query contains only a single item, which must be assignable to the result class,
- the result class is
Object[].class, or - the result class is a non-abstract class or record type with a constructor with the same number of parameters as the query has items in its select list, and the constructor parameter types must exactly match the types of the corresponding items in the select list.
In the first case, each query result is returned directly to the caller. In the second case, each query result is packaged in an array with the array elements corresponding by position with the items of the query select list. In the third case, each query result is automatically packaged in a new instance of the result class by calling the matching constructor.
If the result class of a named query is not specified: it defaults to
Objectif the query has a single item in its select list or toObject[]if the query has multiple items in its select list. When generating the canonical static metamodel for a named query, the generator is permitted to infer a more precise type from the items in the select list.The result class may be overridden by explicitly passing a class object to
EntityHandler.createNamedQuery(String, Class).- Since:
- 3.2
- Default:
void.class
-
lockMode
LockModeType lockMode(Optional) The lock mode type to use in query execution. If alockModeother thanLockModeType.NONEis specified, the query must be executed in a transaction and the persistence context joined to the transaction.- Since:
- 2.0
- See Also:
- Default:
NONE
-
lockScope
PessimisticLockScope lockScope(Optional) The pessimistic lock scope to use in query execution if a pessimistic lock mode is specified vialockMode().- Since:
- 4.0
- Default:
NORMAL
-
hints
QueryHint[] hints(Optional) Query properties and hints. May include vendor-specific query hints.- See Also:
- Default:
{}
-
entityGraph
String entityGraph(Optional) The name of a named entity graph interpreted as a load graph and applied to the entity returned by the query. The namedEntityGraphmay be overridden by callingTypedQuery.setEntityGraph(EntityGraph).- Since:
- 4.0
- Default:
""
-