Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

JavaHibernateHql

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

Use Query.setParameterList(), Javadoc here.

There are four variants to pick from.

Solution 2 - Java

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");

Solution 3 - Java

In TorpedoQuery it look like this

Entity from = from(Entity.class);
where(from.getCode()).in("Joe", "Bob");
Query<Entity> select = select(from);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkarlgrzView Question on Stackoverflow
Solution 1 - JavaJason CohenView Answer on Stackoverflow
Solution 2 - JavaSteve KuoView Answer on Stackoverflow
Solution 3 - JavaxjodoinView Answer on Stackoverflow