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

我们两清 提交于 2019-12-17 08:54:57

问题


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?


回答1:


Use Query.setParameterList(), Javadoc here.

There are four variants to pick from.




回答2:


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:


In TorpedoQuery it look like this

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


来源:https://stackoverflow.com/questions/570229/hibernate-hql-query-how-to-set-a-collection-as-a-named-parameter-of-a-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!