persistence@glassfish.java.net

Re: Cannot use entities in IN (WHERE) clause?

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Mon, 8 Oct 2007 11:24:54 +0200 (CEST)

Hi Chris,

On Mon, October 8, 2007 11:05, Krzysztof Adamczyk wrote:
> Is this a bug that I cannot use such a syntax:
> "... and e.contractor IN (contr1, contr2, contr3...)..."
> in EJBQL, or I need some workaround?
>
> The generated SQL for DB2 is missing the column name and looks like this:
> "... AND IN(...)..."
>
> contr1..3 are entities and I assign them to the query with
> Query.setParameter(String, Object).
>
> If I do the same with Strings, it works, for example
> " and e.shortName IN ('aa', 'bb', 'cc)... "

The EJB 3.0 Persistence specifications says this about IN in section 4.6.8:

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

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

The state_field_path_expression must have a string, numeric, or enum value.

The literal and/or input_parameter values must be like the same abstract
schema type of the state_field_path_expression in type. (See Section 4.12).
----------------------------------------------

In other words, IN only works on string, numbers and enums on the left hand
side and the right hand side must have literal or input parameters of the same
type as on the left hand side.

This confirms your findings : entities are not allowed on the left hand side,
so they are not allowed to the right hand side since the right hand side must
have the same type as the left hand side.


HTH, Wouter van Reeven

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"