问个hibernate sqlquery 的问题

qw4286874 2013-08-30 11:27:10

String ids = "1,2,3";
StringBuffer sql = new StringBuffer("delete from t_client where id in (:clientids)");
SQLQuery sqlQuery = getSession().createSQLQuery(sql.toString());
sqlQuery.setParameter("clientids", ids);
int result = sqlQuery.executeUpdate();


现在的问题是我如果sql中直接delete from t_client where id in ("+ids+")这样写就能删除3条。
但是我用setParameter设置参数只能删除1条。求原因。。。。
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qw4286874 2013-08-30
  • 打赏
  • 举报
回复
找到问题了。。。。 String[] ids = new String[]{1,2,3}; 然后使用sqlQuery.setParameterList("ids", ids); 就可以了。。。。。。必须用数组。。。
骑士的崛起 2013-08-30
  • 打赏
  • 举报
回复
不管有没有单引号,数据库预编译delete from t_client where id in (?); 然后你有传了个参数1,2,3,肯定数据库理解是一个参数。
qw4286874 2013-08-30
  • 打赏
  • 举报
回复
引用 1 楼 zxcvbnm11920 的回复:
你这样写应该一条都删不了吧。 setParameter("clientids", ids); 把sql变成这样了。 delete from t_client where id in ('1,2,3');
最终生成的不带那个单引号。
qw4286874 2013-08-30
  • 打赏
  • 举报
回复
引用 1 楼 zxcvbnm11920 的回复:
你这样写应该一条都删不了吧。 setParameter("clientids", ids); 把sql变成这样了。 delete from t_client where id in ('1,2,3');
最终生成的不带那个单引号。
骑士的崛起 2013-08-30
  • 打赏
  • 举报
回复
你这样写应该一条都删不了吧。 setParameter("clientids", ids); 把sql变成这样了。 delete from t_client where id in ('1,2,3');

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧