67

Given the following HQL Query:

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

I set :id using the Query object's 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?

Mat
  • 188,820
  • 38
  • 367
  • 383
karlgrz
  • 13,425
  • 10
  • 43
  • 57

3 Answers3

93

Use Query.setParameterList(), Javadoc here.

There are four variants to pick from.

Baztoune
  • 1,225
  • 1
  • 16
  • 20
Jason Cohen
  • 75,915
  • 26
  • 104
  • 111
33

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
  • 58,491
  • 75
  • 189
  • 247
1

In TorpedoQuery it look like this

Entity from = from(Entity.class);
where(from.getCode()).in("Joe", "Bob");
Query<Entity> select = select(from);
xjodoin
  • 469
  • 4
  • 14