Java Hibernate HQL 查询:如何将集合设置为查询的命名参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate HQL Query : How to set a Collection as a named parameter of a Query?
提问by karlgrz
Given the following HQL Query:
鉴于以下 HQL 查询:
FROM
Foo
WHERE
Id = :id AND
Bar IN (:barList)
I set :id
using the Query object's setInteger()
method.
我:id
使用 Query 对象的setInteger()
方法进行设置。
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?
我想:barList
使用 a List
of 对象进行设置,但是查看 Hibernate 文档和方法列表,我看不到使用哪个的明显选择。有任何想法吗?
采纳答案by Jason Cohen
Use Query.setParameterList()
, Javadoc here.
使用Query.setParameterList()
,的Javadoc在这里。
There are four variants to pick from.
有四种变体可供选择。
回答by Steve Kuo
I'm not sure about HQL, but in JPA you just call the query's setParameter
with the parameter and collection.
我不确定 HQL,但在 JPA 中,您只需setParameter
使用参数和集合调用查询。
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
names
您要搜索的名称集合在哪里
Collection<String> names = new ArrayList<String();
names.add("Joe");
names.add("Jane");
names.add("Bob");
回答by xjodoin
In TorpedoQueryit look like this
在TorpedoQuery 中它看起来像这样
Entity from = from(Entity.class);
where(from.getCode()).in("Joe", "Bob");
Query<Entity> select = select(from);