因此,我直接从数据库中检索了一个对象列表,并希望将其删除。但是,从文档中看,我似乎必须在一个字符串中构造整个SQL查询,并将该字符串传递给createQuery方法以进行删除。有没有更好的方法呢?
List<Foo> confirm = (List<Foo>) criteria.list();
session.delete(confirm.get(0)); //Works for single object,
//what about batching?发布于 2013-01-08 03:10:29
您可以遍历实体并为每个实体调用delete,也可以构造一个删除查询:
String hql = "delete from Foo f where f.id in :fooIds";
session.createQuery(hql).setParameterList("fooIds", fooIds).executeUpdate();但是,请确保了解与此类DML查询相关的限制和注意事项。它们在the documentation中进行了描述。
发布于 2013-09-16 14:16:32
String hql = "delete from Foo f where f.id in :fooIds";应该是String hql = "delete from Foo f where f.id in (:fooIds)";,否则这样的异常将抛出:"Exception in thread "main“org.hibernate.hql.ast.QuerySyntaxException: exception:”我的hibernate版本是3.6.0最终版本。
https://stackoverflow.com/questions/14200630
复制相似问题