执行“MEMBER OF”针对“ElementCollection”的查询JP-QL (JPA 2.0) 中的映射字段
是否可以对关联数组运行“MEMBER OF”查询?如果是这样,语法是什么样的?明显的解决方法是使用本机查询,但由于所有连接等,这会变得非常混乱。我想测试地图的键集、值集合或条目集中是否存在对象。也许类似于以下内容:
SELECT p FROM Person p WHERE 'home' MEMBER OF p.phoneNumbers.keySet
SELECT p FROM Person p WHERE '867-5309' MEMBER OF p.phoneNumbers.values
SELECT p FROM Person p WHERE {'home' -> '867-5309'} MEMBER OF p.phoneNumbers
与提供者无关的代码可能要求太多; Eclipselink 支持这个吗?
Is it possible to run a "MEMBER OF" query against associative arrays? If so, what does the syntax look like? The obvious workaround is a native query but that gets pretty messy what with all the joins and such. I'd like to test for existence of an object within the map's key set, value collection or entry set. Maybe something like the following:
SELECT p FROM Person p WHERE 'home' MEMBER OF p.phoneNumbers.keySet
SELECT p FROM Person p WHERE '867-5309' MEMBER OF p.phoneNumbers.values
SELECT p FROM Person p WHERE {'home' -> '867-5309'} MEMBER OF p.phoneNumbers
Provider-agnostic code might be too much to ask for; does Eclipselink support this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JPQL 有一个名为
index()
的函数,它对于获取@OrderColumn
列表中的索引非常有用。正如您自己所说,映射也称为关联数组,映射键对应于数组索引。因此,没有什么可以阻止index()
返回映射条目的键。此查询在休眠状态下完美运行:
JPQL has a function named
index()
which is useful for getting the index in an@OrderColumn
list. And as you said yourself, maps are also called associative arrays and map keys correspond to array indices. So nothing preventsindex()
from returning the key of a map entry.This query works perfectly on hibernate:
它应该有效:
下一个查询对我有用:
It should work:
The next query works for me:
JPA(2) 规范没有定义 MEMBER OF 中使用 Map 的语法(您最初指的是数组,但如果您有地图,我看不到相关性),因此您不能依赖任何对所有 JPA 实现都有效的语法。由于 JPQL 不支持像 keySet、values 这样的 Java 方法,所以我看不到这些方法是可能的。它很可能只支持检查值是否存在,例如
作为参考,在 JDOQL 中,您会这样做
The JPA(2) spec doesn't define its syntax for a Map use in MEMBER OF (you were referring originally to arrays but I don't see the relevance if you have a map), consequently you can't rely on any syntax being valid for all JPA implementations. Since JPQL doesn't support Java methods like keySet, values then I can't see those being likely. More than likely it will only support the check for a value existence, like
For reference, in JDOQL, you would do