Annotation Interface ConstructorResult
SqlResultSetMapping or
NamedNativeQuery annotation to map the SELECT
clause of a SQL query to the constructor of an arbitrary
Java class.
When processing a result set, the provider instantiates
the target class by calling a
matching constructor of the class, passing as arguments the
values of the specified columns() of the result
set. Columns must be explicitly listed by columns()
in the same order as their corresponding parameters occur in
the argument list of the constructor.
The target class need not be a managed type. Any instance of an entity class returned as a constructor result will be in either the new or detached state, depending on whether a primary key was assigned to the constructed object.
Consider the following SQL query:
Query customerDetails =
em.createNativeQuery(
"""
SELECT c.id, c.name,
COUNT(o) as order_count,
AVG(o.price) AS avg_price
FROM Customer c
JOIN Orders o ON o.cid = c.id
GROUP BY c.id, c.name
""",
ResultMappings_.CUSTOMER_DETAILS
);
The result set mapping might be defined as follows:
@SqlResultSetMapping(
name = "CustomerDetails",
classes = @ConstructorResult(
targetClass = CustomerDetails.class,
columns = {
@ColumnResult(name = "id"),
@ColumnResult(name = "name"),
@ColumnResult(name = "order_count"),
@ColumnResult(name = "avg_price",
type = Double.class)
}
)
)
interface ResultMappings {}
At runtime, a ConstructorResult annotation is represented by an
instance of ConstructorMapping
in the ResultSetMapping
returned by EntityManagerFactory.getResultSetMappings(Class).
This annotation may be placed directly on a method annotated
NativeQuery.
@NativeQuery("SELECT pub_id, count(*) AS book_count FROM books GROUP BY pub_id")
@ConstructorResult(
targetClass = PublisherBookCount.class,
columns = {@ColumnResult("pub_id"),
@ColumnResult("book_count")}
)
List<PublisherBookCount> publisherBookCounts(String pattern);
- Since:
- 2.1
- See Also:
-
Nested Class Summary
Nested Classes -
Required Element Summary
Required ElementsModifier and TypeRequired ElementDescriptionClass<?> (Required) The class whose constructor is to be invoked. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescription(Optional) The mapping of columns in theSELECTlist to arguments of a constructor of the specified Java target class, in order.(Optional) The mapping of columns in theSELECTlist to entity results which are then assigned to parameters of a constructor of the specified Java target class, in order.
-
Element Details
-
targetClass
-
columns
ColumnResult[] columns(Optional) The mapping of columns in theSELECTlist to arguments of a constructor of the specified Java target class, in order.Constructor parameters mapping directly to column results must occur before parameters mapping to entities.
- Default:
{}
-
entities
EntityResult[] entities(Optional) The mapping of columns in theSELECTlist to entity results which are then assigned to parameters of a constructor of the specified Java target class, in order.Constructor parameters mapping to entity results must occur after parameters mapping to columns.
- Since:
- 4.0
- Default:
{}
-