执行“MEMBER OF”针对“ElementCollection”的查询JP-QL (JPA 2.0) 中的映射字段

发布于 2024-08-07 05:49:47 字数 404 浏览 4 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-08-14 05:49:47

JPQL 有一个名为 index() 的函数,它对于获取 @OrderColumn 列表中的索引非常有用。正如您自己所说,映射也称为关联数组,映射键对应于数组索引。因此,没有什么可以阻止 index() 返回映射条目的键。

此查询在休眠状态下完美运行:

SELECT p FROM Person p, in (p.phoneNumbers) number 
WHERE number = '867-5309' AND index(number) = 'home'

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 prevents index() from returning the key of a map entry.

This query works perfectly on hibernate:

SELECT p FROM Person p, in (p.phoneNumbers) number 
WHERE number = '867-5309' AND index(number) = 'home'
绾颜 2024-08-14 05:49:47

它应该有效:

SELECT p FROM Person p 
WHERE (select count(*) from p.phoneNumbers where name='home' and value='867-5309') > 0

下一个查询对我有用:

select model from AnsOutboxMsg model 
left join fetch model.updateEntity upde 
left join fetch upde.update upd 
left join fetch model.ansMessage msg 
left join fetch msg.template msgt 
left join fetch msg.ansAction act 
left join fetch act.ansRule rl 
left join fetch rl.app app  
where (select count(*) from model.contextMap where name = 'fltClient' and value = 'XXX') > 0 and model.status = 'sent' and app.id = 'SPN_TICKETS' and msgt.name = 'Client' order by model.modifDate DESC, model.id ASC

It should work:

SELECT p FROM Person p 
WHERE (select count(*) from p.phoneNumbers where name='home' and value='867-5309') > 0

The next query works for me:

select model from AnsOutboxMsg model 
left join fetch model.updateEntity upde 
left join fetch upde.update upd 
left join fetch model.ansMessage msg 
left join fetch msg.template msgt 
left join fetch msg.ansAction act 
left join fetch act.ansRule rl 
left join fetch rl.app app  
where (select count(*) from model.contextMap where name = 'fltClient' and value = 'XXX') > 0 and model.status = 'sent' and app.id = 'SPN_TICKETS' and msgt.name = 'Client' order by model.modifDate DESC, model.id ASC
情丝乱 2024-08-14 05:49:47

JPA(2) 规范没有定义 MEMBER OF 中使用 Map 的语法(您最初指的是数组,但如果您有地图,我看不到相关性),因此您不能依赖任何对所有 JPA 实现都有效的语法。由于 JPQL 不支持像 keySet、values 这样的 Java 方法,所以我看不到这些方法是可能的。它很可能只支持检查值是否存在,例如

'867-5309' MEMBER OF p.phoneNumbers

作为参考,在 JDOQL 中,您会这样做

p.phoneNumbers.containsKey('home');
p.phoneNumbers.containsValue('867-5309');

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

'867-5309' MEMBER OF p.phoneNumbers

For reference, in JDOQL, you would do

p.phoneNumbers.containsKey('home');
p.phoneNumbers.containsValue('867-5309');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文