Annotation Interface EntityResult
SqlResultSetMapping or
NamedNativeQuery annotation to map the SELECT
clause of a SQL query to an entity result.
The SQL SELECT statement should select every column
mapped by the entity class, including
foreign key columns of related entities. If a mapped column is
missing from the SQL result set, the behavior is undefined.
If the names of the columns of the result set of the SQL
statement exactly match the column names mapped by the entity
class, then it is not necessary to explicitly specify mappings
for the fields() or discriminator column of the entity. Otherwise, if a column name
of the SQL result set does not exactly match the column name
mapped by the entity class, the FieldResult annotation
must be used to explicitly specify the mapping.
Consider the following SQL query:
Query ordersWithItems =
em.createNativeQuery(
"""
SELECT o.id, o.quantity, o.item,
i.id, i.name, i.description
FROM Order o
JOIN Item i ON o.item = i.id
WHERE o.quantity > 25
""",
ResultMappings_.MAPPING_ORDERS_WITH_ITEMS
);
The result set mapping might be defined as follows:
@SqlResultSetMapping(
name = "OrdersWithItems",
entities = {
@EntityResult(entityClass = Order.class),
@EntityResult(entityClass = Item.class)
}
)
interface ResultMappings {}
At runtime, an EntityResult annotation is represented by an
instance of EntityMapping
in the ResultSetMapping
returned by EntityManagerFactory.getResultSetMappings(Class).
This annotation may be placed directly on a method annotated
NativeQuery.
@NativeQuery("SELECT * FROM orders WHERE order_total > ?")
@EntityResult(
entityClass = Order.class,
fields = {@FieldResult(name = Order_.ID, column = "order_id"),
@FieldResult(name = Order_.TOTAL, column = "order_total"),
@FieldResult(name = Order_.ITEM, column = "order_item")}
)
List<Order> largeOrders(int threshold) {
return entityManager.createQuery(Shop_.largeOrders(threshold))
.getResultList();
}
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested Classes -
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionSpecifies the column name (or alias) of the column in theSELECTlist that is used to determine the type of the entity instance.Maps the columns specified in theSELECTlist of the query to the properties or fields of the entity class.The lock mode obtained by the SQL query.
-
Element Details
-
entityClass
Class<?> entityClassThe class of the result. -
lockMode
-
fields
FieldResult[] fieldsMaps the columns specified in theSELECTlist of the query to the properties or fields of the entity class.- Default:
{}
-
discriminatorColumn
String discriminatorColumnSpecifies the column name (or alias) of the column in theSELECTlist that is used to determine the type of the entity instance. An empty string indicates that there is no discriminator column.- Default:
""
-