执行"MEMBER OF"针对JP-QL(JPA 2.0)中的"ElementCollection"映射字段进行查询 [英] Execute "MEMBER OF" query against 'ElementCollection' Map fields in JP-QL (JPA 2.0)

查看:65
本文介绍了执行"MEMBER OF"针对JP-QL(JPA 2.0)中的"ElementCollection"映射字段进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对关联数组运行"MEMBER OF"查询?如果是这样,语法是什么样的?显而易见的解决方法是本机查询,但是对于所有联接等,都变得非常混乱.我想测试地图的键集,值集合或条目集中是否存在对象.也许像下面这样:

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

与提供商无关的代码可能要求太多; Eclipselink支持吗?

Provider-agnostic code might be too much to ask for; does Eclipselink support this?

推荐答案

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 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'

这篇关于执行"MEMBER OF"针对JP-QL(JPA 2.0)中的"ElementCollection"映射字段进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆