Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate batch delete

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?
like image 698
OckhamsRazor Avatar asked Dec 20 '22 11:12

OckhamsRazor


1 Answers

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.

like image 197
JB Nizet Avatar answered Feb 11 '23 15:02

JB Nizet