for example, if there is an @ElementCollection
file which is with a Map
type, then if I try to get the map key or value field then how to process?
Class Deal{
.....
private String name;
private String department;
private DealType type;
@AttributeOverrides({
@AttributeOverride(name="value.in.available", column=@Column(name="in_avl")),
@AttributeOverride(name="value.in.unavailable", column=@Column(name="in_unv")),
@AttributeOverride(name="value.out.available", column=@Column(name="out_avl")),
@AttributeOverride(name="value.out.unavailable", column=@Column(name="out_unv"))
})
@ElementCollection(fetch = FetchType.EAGER)
......
}
So if I try to get something like this
select new SummaryAmount(SUM(t.value.in.available), SUM(t.value.in.unavailable),
SUM(t.value.out.available), SUM(t.value.out.unavailable)) from Deal AS d INNER
JOIN d.transactionAmounts t GROUP by t.key;
Is it something possible can work out now? Everything is follow the book except I invent the t.value
and t.key
as I really don't know how to present map key and value in JPQL.Thanks
Thanks
Try this:
SELECT new SummaryAmount(SUM(VALUE(t).in.available), SUM(VALUE(t)in.unavailable),
SUM(VALUE(t).out.available), SUM(VALUE(t).out.unavailable)) from Deal AS d INNER
JOIN d.transactionAmounts t GROUP by KEY(t);
And now an excerpt from the JPA specification:
An identification variable qualified by the KEY, VALUE, or ENTRY
operator is a path expression. The KEY, VALUE, and ENTRY operators may
only be applied to identification variables that correspond to
map-valued associations or map-valued element collections. The type of
the path expression is the type computed as the result of the
operation; that is, the abstract schema type of the field that is the
value of the KEY, VALUE, or ENTRY operator (the map key, map value, or
map entry respectively).[53]
The syntax for qualified identification variables is as follows.
qualified_identification_variable :: =
KEY(identification_variable) |
VALUE(identification_variable) |
ENTRY(identification_variable)
A path expression using the KEY or VALUE operator can be further
composed. A path expression using the ENTRY operator is terminal. It
cannot be further composed and can only appear in the SELECT list of a
query.