0

I want to delete the list of Object with the getCurrentSession().delete() method. I found the optional way by writing the query like

getCurrentSession().createQuery("delete from Student where studentId =1").executeUpdate();

And I have come to another solution like

for (Student students : listOfStudents) {
    getCurrentSession().delete(student);
}

But it's not a database efficient call as it will create as many queries as the size of list. Is there any other efficient way to delete the list except writing a Hibernate query?

Laurel
  • 5,965
  • 14
  • 31
  • 57
UtkarshBhavsar
  • 249
  • 3
  • 23
  • What's wrong with using the query solution that you first mentioned? Do you just not have the flexibility to compose the SQL? – raminr Mar 13 '15 at 02:54
  • I agree to writing HQL. But is there other way to delete the list? – UtkarshBhavsar Mar 13 '15 at 06:45
  • I am not aware of such capability. I think what you are suggesting is this: put a bunch of entities in the session and then call a hibernate function to collectively delete all those entities in one SQL. The issue is that those entities could map to different tables in the DB. I think you can see the problem with that. – raminr Mar 13 '15 at 14:09

0 Answers0