简体   繁体   English

如何在HQL查询中使用setString()设置IN操作数

[英]How to set IN operand using setString() in HQL query

We can set id if sql is like 如果sql像

select * from user where id = :id

using setString("id", "0") 使用setString("id", "0")

How can I set id when it is like 我怎样才能设置id

select * from user where id in (0,2)

in HQL query. 在HQL查询中。

You can try this way: 您可以这样尝试:

Get preparedStatement by passing sql query string as "select * from user where id in (?,?)" and then set the values for id as: 通过将sql查询字符串传递为“ select id from user where id in(?,?)”来获得prepareStatement,然后将id的值设置为:

    stmt.setInt(0);
    stmt.setInt(2);

where stmt is an instance of PreparedStatement . 其中,stmt是PreparedStatement的实例。

EDIT for HQL: 编辑HQL:

With HQL you can query it as: 使用HQL,您可以查询为:

    Query q = session.createQuery("from user u where id IN (:ids)");
    q.setParameterList("ids", idList);

where idList is List of ids you want to pass. 其中idList是要传递的ID列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM