So I retrieved a List of objects directly from my database and wish to delete them. However, from the docs, it looks like I have to construct the entire SQL query in a string, and pass that string to the createQuery method in order to delete. Isn't there a better way to do it?
List<Foo> confirm = (List<Foo>) criteria.list();
session.delete(confirm.get(0)); //Works for single object,
//what about batching?
Either you iterate over the entities and call delete for each of them, or you construct a delete query:
String hql = "delete from Foo f where f.id in :fooIds";
session.createQuery(hql).setParameterList("fooIds", fooIds).executeUpdate();
Make sure to understand the restrictions and caveats associated to such DML queries though. They're described in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With