Hibernate removal

So, I got a list of objects directly from my database and I want to delete them. However, from the docs, it looks like I have to build the entire SQL query in a row and pass that row to the createQuery method for deletion. Isn't there a better way to do this?

List<Foo> confirm = (List<Foo>) criteria.list(); session.delete(confirm.get(0)); //Works for single object, //what about batching? 
+4
source share
2 answers

Either you iterate over the entities and call delete for each of them, or build a delete request:

 String hql = "delete from Foo f where f.id in :fooIds"; session.createQuery(hql).setParameterList("fooIds", fooIds).executeUpdate(); 

Be sure to review the limitations and caveats associated with such DML queries. They are described in the documentation .

+8
source

String hql = "delete from Foo f where f.id in :fooIds"; should be String hql = "delete from Foo f where f.id in (:fooIds)"; or such an exception throws: "An exception in the stream" main "org.hibernate.hql.ast.QuerySyntaxException: unexpected token: my version of sleep mode is 3.6.0. Final.

+3
source

All Articles