如何在jpql中为in-clause设置集合项? [英] How to set collection items for in-clause in jpql?

查看:80
本文介绍了如何在jpql中为in-clause设置集合项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA 2.0中是否有可能在jpql-query中为in-clause设置集合?
(我正在使用EclipseLink)

Is there a possiblity in JPA 2.0 to set a collection for in-clause in jpql-query? (I'm using EclipseLink)

下一个示例失败:

TypedQuery<Person> q = em.createQuery("select p from Person p where p.name in (?1)", Person.class);

List<String> names = Arrays.asList(new String[] { "Bill Gates", "Steve Jobs" });
// THIS FAILS
q.setParameter(1, names);

List<Person> persons = q.getResultList();
for (Person p: persons) {
    System.out.println(p.getName());
}

还有其他办法吗?

推荐答案

以下是JPA 2.0规范中关于IN表达式的内容:

Here is what the JPA 2.0 specification says about IN expressions:


4.6.9在表达式中




条件表达式中使用
比较运算符[NOT] IN的语法如下:

4.6.9 In Expressions

The syntax for the use of the comparison operator [NOT] IN in a conditional expression is as follows:

in_expression ::=
    {state_field_path_expression | type_discriminator} [NOT] IN
        { ( in_item {, in_item}* ) | (subquery) | collection_valued_input_parameter }
in_item ::= literal | single_valued_input_parameter

...

因此,根据规范,传递 collection_valued_input_parameter 时的正确语法不带括号:

So according to the specification, the correct syntax when passing a collection_valued_input_parameter is without parenthesis:

select p from Person p where p.name in ?1

这适用于EclipseLink 。

And this works with EclipseLink.

这篇关于如何在jpql中为in-clause设置集合项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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