【问题标题】:How to set list of values as parameter into hibernate query?如何将值列表设置为休眠查询的参数?
【发布时间】:2011-09-28 22:03:08
【问题描述】:

例如,我有这个查询

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

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

此代码不起作用

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

因此,我希望有像id in (1,2,3,4) 这样的 SQL 查询

【问题讨论】:

    标签: sql hibernate jpa


    【解决方案1】:

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

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

    【讨论】:

    • setParameterList 在较新的休眠模式中已被废弃。我们如何传递这个?我尝试使用 setparameter 并没有得到正确的行为......
    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2018-10-06
    • 2017-10-15
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多