Hi Markus -
But I have no idea in advance how many things might actually be in the
parameter set, so I can't pre-define the query with 3 parameters --
sometimes there might be one, sometimes 10...
I guess I'll have to do it differently. I'm actually porting some code
from Hibernate; Hibernate allows you to call q.setParameter("listID", o)
where o is a collection of strings. I figured out where the JPA spec
requires o to be the same type as the id (so a string), but I was hoping
I could just construct a single string with all the values.
-Scott
On Fri, 2007-04-13 at 13:53, Markus Fuchs wrote:
> Hi Scott,
>
> The spec specifies the IN expression as:
>
> in_expression ::=
> state_field_path_expression [NOT] IN ( in_item {, in_item}* | subquery)
>
> where in_item can be a parameter. So, in your case, you'd use a
> parameter marker for each country:
>
> @NamedQuery(name="findByCountry", query="SELECT c FROM Country c WHERE
> c.id IN(?1, ?2, ?3)")
>
> q.setParameter(1, "UK").setParameter(2, "US").setParameter(3, "France");
>
> Also notice that the parameter in your example is declared and used in
> different cases: "listId" and "listID".
>
> Thanks,
>
> -- markus.
>
> Scott Oaks wrote:
> > I'm trying to use a named query:
> >
> > @NamedQuery(name="findByCountry", query="SELECT c FROM Country c WHERE
> > c.id IN(:listId)")
> >
> > I want to set the parameter to
> >
> > 'UK', 'US', 'France'
> >
> > But nothing I try works. If I do
> > q.setParameter("listId", "US")
> > then I get back the results with only the US (so I know the rest of the
> > code is okay). But q.setParameter("listID", "'UK', 'US', 'France'")
> > fails to retrieve anything, as does q.setParameter("listID", "UK, US,
> > France") and q.setParameter("\"UK\", \"US\", \"France\").
> >
> > -Scott
> >