persistence@glassfish.java.net

Re: Use of parameters in IN statement

From: Michael Bouschen <mbo.tech_at_spree.de>
Date: Fri, 30 Oct 2009 23:02:29 +0100

Hi,

are you using a JPA 1.0 or JPA 2.0 implementation?

The IN-expression has been extended in JPA 2.0
  in_expression ::=
    {state_field_path_expression | type_discriminator} [NOT] IN
    { ( in_item {, in_item}* ) | (subquery) |
collection_valued_input_parameter }

Now you can use a collection valued input parameter, such as
  ... a.type IN :types
and types is e.g. a collection of strings. Please note, in this case
there are no parenthesis around :types.

The above is not supported in JPA 1.0.

Regards Michael

> Hi,
>
> is :type a collection or a single value? The Java Persistence Query
> Language does not support collections as part of the IN expression.
> But if :type is a single value, then the expression
> a.type IN ( :type )
> is the same as
> a.type = :type
>
> Section "4.6.8 In Expressions" of the Java Persistence API
> specification defines the IN expression:
> in_expression ::=
> state_field_path_expression [NOT] IN ( in_item {, in_item}* |
> subquery)
> in_item ::= literal | input_parameter
>
> This means you can use an input parameter in the list of IN-expression
> items, but it must have the same type the expression left of the IN
> keyword. So if 'a.type' is a string, the following expression is legal
> if the parameter is of type string:
> ... a.type IN ('int', 'float', :type) ...
>
> If this is what you are trying, please run the query in debug mode and
> check the generated SQL including the binding of the SQL input
> parameters whether the correct valued gets bound.
>
> I hope this helps.
>
> Regards Michael
>
>> Dear colleagues,
>>
>> Does EJBQL grammar support named/positional in the IN statement?
>>
>> According to 4.6.4 Input Parameters of spec:
>>
>> "Input parameters can only be used in the WHERE clause or HAVING
>> clause of a query."
>>
>> Since an IN expression is within a WHERE clause I assumed I could use
>> parameter within the IN statement.
>>
>> However, when I use the :type parameter in query below I get no
>> errors and no matches (I was expecting a match):
>>
>> SELECT DISTINCT Object(p) FROM PersonType p, AssociationType a WHERE
>> (a.sourceObject = 'urn:wellfleetsoftware:Association:farrukhNajmi'
>> AND
>> p.id = a.targetObject AND
>> a.type IN ( :type ))
>>
>> When I replace :type in above with the parameter value in the query
>> (hard code it) then all works as expected and I get some matches.
>>
>> Is this expected? If so where is it defined in the spec? Thanks.
>>
>
>
> --
> *Michael Bouschen*
> *Prokurist*
>
> akquinet tech_at_spree GmbH
> Bülowstr. 66, D-10783 Berlin
>
> Fon: +49 30 235 520-33
> Fax: +49 30 217 520-12
> Email: michael.bouschen_at_akquinet.de
> Url: www.akquinet.de <http://www.akquinet.de>
>
> akquinet tech_at_spree GmbH, Berlin
> Geschäftsführung: Martin Weber, Prof. Dr. Christian Roth
> Amtsgericht Berlin-Charlottenburg HRB 86780 B
> USt.-Id. Nr.: DE 225 964 680


-- 
*Michael Bouschen*
*Prokurist*
akquinet tech_at_spree GmbH
Bülowstr. 66, D-10783 Berlin
Fon:   +49 30 235 520-33
Fax:   +49 30 217 520-12
Email: michael.bouschen_at_akquinet.de
Url:    www.akquinet.de <http://www.akquinet.de>
akquinet tech_at_spree GmbH, Berlin
Geschäftsführung: Martin Weber, Prof. Dr. Christian Roth
Amtsgericht Berlin-Charlottenburg HRB 86780 B
USt.-Id. Nr.: DE 225 964 680