SQL 如何将值列表作为参数设置为休眠查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6584898/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:12:57  来源:igfitidea点击:

How to set list of values as parameter into hibernate query?

sqlhibernatejpa

提问by yura

For example, I have this query

例如,我有这个查询

 select cat from Cat cat where cat.id in :ids 

and I want to set ids to list (1,2,3,4,5,6,17,19).

我想将 ids 设置为列表 (1,2,3,4,5,6,17,19)。

This code doesn't work

此代码不起作用

session.createQuery("select cat from Cat cat where cat.id in :ids")
       .setParameter("ids", new Long[]{1,2,3,4,5})

As the result I would like to have SQL query like id in (1,2,3,4)

结果我想要像这样的 SQL 查询 id in (1,2,3,4)

回答by sblundy

Use setParameterList(). You'll also have to put parenthesis around the list param.

使用setParameterList(). 您还必须在列表参数周围加上括号。

session.createQuery("select cat from Cat cat where cat.id in (:ids)").setParameterList("ids", new Long[]{1,2,3,4,5})