NamedNativeQuery
Target: TYPE
- Implemented Interfaces:
Annotation
Declares a named native SQL query and, optionally, the mapping of the result of the native SQL query. Query names are scoped to the persistence unit. A named query may be executed by calling createNamedQuery().
In simple cases, a NamedNativeQuery.resultClass specifies how the native SQL query result set should be interpreted, for example:
@NamedNativeQuery( name = "findWidgets", query = "SELECT o.id, o.quantity, o.item " + "FROM Order o, Item i " + "WHERE (o.item = i.id) AND (i.name = 39;widget39;)", resultClass = com.acme.Order.class )
In more complicated cases, a plain is needed, which may be specified using either a separate annotation:
@NamedNativeQuery( name = "OrderItems", query = "SELECT o.id, o.quantity, o.item, i.id, i.name, i.description " + "FROM Order o, Item i " + "WHERE (o.quantity > 25) AND (o.item = i.id)", resultSetMapping = "OrderItemResults" ) @SqlResultSetMapping(name="OrderItemResults", entities={ @EntityResult(entityClass=com.acme.Order.class), @EntityResult(entityClass=com.acme.Item.class) })or using the elements of this annotation:
@NamedNativeQuery( name = "OrderItems", query = "SELECT o.id, o.quantity, o.item, i.id, i.name, i.description " + "FROM Order o, Item i " + "WHERE (o.quantity > 25) AND (o.item = i.id)", resultSetMapping = "OrderItemResults"); entities={ @EntityResult(entityClass=com.acme.Order.class), @EntityResult(entityClass=com.acme.Item.class) } )
The NamedNativeQuery annotation can be applied to an entity class or mapped superclass.
- See Also:
- SqlResultSetMapping
- Since:
- JPA 1.0
Public Annotation Attributes
May not be used in combination with NamedNativeQuery.resultSetMapping.
- Since:
- JPA 3.2
May not be used in combination with NamedNativeQuery.resultSetMapping.
- Since:
- JPA 3.2
May not be used in combination with NamedNativeQuery.resultSetMapping.
- Since:
- JPA 3.2
String name default null
- Since:
- JPA 1.0
String query default null
- Since:
- JPA 1.0
Class<?> resultClass default void.class
If a result set mapping is specified, the specified result class must agree with the type inferred from the result set mapping. If a
resultClass is not explicitly specified, then it is inferred from the result set mapping, if any, or defaults to Object or Object[]. The query result class may be overridden by explicitly passing a class object to createNamedQuery(). - Since:
- JPA 1.0
String resultSetMapping default ""
The named result set mapping is used to interpret the result set of the native SQL query.
Alternatively, the elements NamedNativeQuery.entities, NamedNativeQuery.classes, and NamedNativeQuery.columns may be used to specify a result set mapping. These elements may not be used in conjunction with resultSetMapping.
- Since:
- JPA 1.0