views:

10330

answers:

2

Given the following HQL Query:

FROM
    Foo
WHERE
    Id = :id AND
    Bar IN (:barList)

I set :id using the Query objects' setInteger() method.

I would like to set :barList using a List of objects, but looking at the Hibernate documentation and list of methods I cannot see an obvious choice of which to use. Any ideas?

+4  A: 

I'm not sure about HQL, but in JPA you just call the query's setParameter with the parameter and collection.

Query q = entityManager.createQuery("SELECT p FROM Peron p WHERE name IN (:names)");
q.setParameter("names", names);

where names is the collection of names you're searching for

Collection<String> names = new ArrayList<String();
names.add("Joe");
names.add("Jane");
names.add("Bob");
Steve Kuo
+11  A: 

Use Query.setParameterList(), [Javadoc here][1].

There are four variants to pick from.

[1]: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Query.html#setParameterList(java.lang.String, java.util.Collection)

Jason Cohen
Thank you for pointing this out! I completely overlooked this when looking at the the JavaDoc.
KG
D: Oh noes, broken link (...painfully typical of the JBoss docs)
Matt Ball