如何通过JPQL访问JPA中的“地图"字段 [英] How to access Map field in JPA via JPQL
问题描述
例如,如果有一个@ElementCollection
文件具有Map
类型,那么如果我尝试获取map键或value字段,该如何处理?
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;
现在有可能解决吗?除了我发明了t.value
和t.key
之外,其他所有内容都遵循本书,因为我真的不知道如何在JPQL中显示映射键和值.谢谢
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
谢谢
推荐答案
尝试一下:
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);
现在摘录自JPA规范:
And now an excerpt from the JPA specification:
由KEY,VALUE或ENTRY限定的标识变量 运算符是路径表达式. KEY,VALUE和ENTRY运算子可以 仅适用于与 地图值关联或地图值元素集合.的类型 路径表达式是根据 手术;也就是说,该字段的抽象架构类型为 KEY,VALUE或ENTRY运算符的值(地图键,地图值或 分别输入地图).[53]
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 :: =
qualified_identification_variable :: =
KEY (identification_variable)|
KEY(identification_variable) |
VALUE (identification_variable)|
VALUE(identification_variable) |
ENTRY (identification_variable)
ENTRY(identification_variable)
使用KEY或VALUE运算符的路径表达式可以更进一步 组成.使用ENTRY运算符的路径表达式是terminal.它 不能进一步构成,只能出现在列表的SELECT列表中 查询.
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.
这篇关于如何通过JPQL访问JPA中的“地图"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!