关于hibernate中的setParameter问题,急!!!急!!!急!!!

雨枫 2011-06-03 11:03:07
hibernate中的sql语句是这样的:

SQLQuery query = this.getSession().createSQLQuery("delete from tab where groupId=? and expertsid in (?)");
query.setParameter(0, groupId);
query.setParameter(1, expertIds);

现在的问题是,groupId是单个id,比如说是123456,这个没有问题,但是后面的那个in里面改怎么设置参数,比如说现在有两个id分别是234和456,那么expertids该是一个什么样的字符串。
就事说执行的sql语句应该是
delete from tab where groupId=123456'' and expertsid in ('234','456');

各位大侠给力啊!!!!很急!!!!
...全文
1086 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
雨枫 2011-06-03
  • 打赏
  • 举报
回复
是setParameterList,不是setParameter
雨枫 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hanyu332 的回复:]

虽然记得不太清了,但是我知道hibernate有一个方法是直接setlist的
[/Quote]

恩,试过了,这个也可以,解决办法是:
delete from bas_expert_link_group where groupId=:groupid and expertsid in (:ids)

expertsid = "234,456";
setParameter("ids",(Object[])expertsid.split(","));
雨枫 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zjt321 的回复:]
1: 改下hql语句
String hql = "delete from tab where groupId=? and expertsid in ("+expertIds+")";

2:
List<String> list = new ArrayList<String>();
list.add("1234");
list.add("3444");
用 setParameterLi……
[/Quote]
谢谢,解决了!!
这个办法可以做到,非常感谢!!
Bannings 2011-06-03
  • 打赏
  • 举报
回复
setParameterList
zjt321 2011-06-03
  • 打赏
  • 举报
回复

1: 改下hql语句
String hql = "delete from tab where groupId=? and expertsid in ("+expertIds+")";

2:
List<String> list = new ArrayList<String>();
list.add("1234");
list.add("3444");
用 setParameterList(1,list);
雨枫 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhangao0086 的回复:]

那你应该在数据库中执行也不会删除
SQL code

delete from tab where groupId='123456' and expertsid in ('234','456')
[/Quote]

数据库执行可以删除,in里面一个参数也能删除,但是如果是一个以上的话就不能删除了,
现在就是不知道setParameter里面怎么设置
比如说第一个可以这样写:setParameter(0,'123');
第二个这样写就不行:setParameter(1,"'234','456'")
hanyu332 2011-06-03
  • 打赏
  • 举报
回复
虽然记得不太清了,但是我知道hibernate有一个方法是直接setlist的
Bannings 2011-06-03
  • 打赏
  • 举报
回复
那你应该在数据库中执行也不会删除

delete from tab where groupId='123456' and expertsid in ('234','456')
雨枫 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhangao0086 的回复:]

"'234','456'"
[/Quote]
就是第二个参数的设置方式应该是:
query.setParameter(1, "'234','456'");

这样不行啊
sql语句执行了,不报错,可是数据库里面仍然有数据。
Bannings 2011-06-03
  • 打赏
  • 举报
回复
"'234','456'"

67,513

社区成员

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

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