How to set collection items for in-clause in jpql?

末鹿安然 提交于 2019-12-05 06:02:46

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

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

...

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

And this works with EclipseLink.

Using EclipseLink, you need to use a name to your paramether.

Ex.:

TypedQuery<Person> q = em.createQuery("select p from Person p where p.name in :names", Person.class);
List<String> names = Arrays.asList(new String[] { "Bill Gates", "Steve Jobs" });
q.setParameter("names", names);

The symbol "?" does not work using clause "in". It was tested in EclipseLink 2.5.1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!