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)
})
@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.
SqlResultSetMapping
| Modifier and Type | Method and Description |
|---|---|
| public ConstructorResult[] | |
| public ColumnResult[] | |
| public EntityResult[] | |
| public QueryHint[] | |
| public String | |
| public String | |
| public Class | |
| public String |
| classes | back to summary |
|---|---|
| public ConstructorResult[] classes() Specifies the result set mapping to constructors.
May not be used in combination with
| |
| columns | back to summary |
|---|---|
| public ColumnResult[] columns() Specifies the result set mapping to scalar values.
May not be used in combination with
| |
| entities | back to summary |
|---|---|
| public EntityResult[] entities() Specifies the result set mapping to entities.
May not be used in combination with
| |
| hints | back to summary |
|---|---|
| public QueryHint[] hints() Query properties and hints. (May include vendor-specific query hints.) | |
| name | back to summary |
|---|---|
| public String name() The name used to identify the query in calls to
| |
| query | back to summary |
|---|---|
| public String query() The native SQL query string. | |
| resultClass | back to summary |
|---|---|
| public Class The class of each query result. If a | |
| resultSetMapping | back to summary |
|---|---|
| public String resultSetMapping() The name of a Alternatively, the elements | |