Annotation Interface NamedNativeQuery


@Repeatable(NamedNativeQueries.class) @Target(TYPE) @Retention(RUNTIME) public @interface NamedNativeQuery
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 EntityManager.createNamedQuery(String, Class).

In simple cases, a 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 = 'widget')",
        resultClass = com.acme.Order.class
)

In more complicated cases, a result set mapping 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.

Since:
1.0
See Also: