Hibernate HQL Query : How to set a Collection as a

2019-01-08 13:02发布

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?

3条回答
Summer. ? 凉城
2楼-- · 2019-01-08 13:47

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");
查看更多
唯我独甜
3楼-- · 2019-01-08 13:50

In TorpedoQuery it look like this

Entity from = from(Entity.class);
where(from.getCode()).in("Joe", "Bob");
Query<Entity> select = select(from);
查看更多
时光不老,我们不散
4楼-- · 2019-01-08 13:56

Use Query.setParameterList(), Javadoc here.

There are four variants to pick from.

查看更多
登录 后发表回答