list = new ArrayList

Delete () and Deleteall () in sleep mode

Consider two ways to delete a set of employees named "John".

List<Employee> list = new ArrayList<Employee>(); String query= " from Employee emp where emp.name = 'John'"; list=getHibernateTemplate().find(query); 

First method:

  getHibernateTemplate().deleteAll(list); 

Second method:

 Iterator<BulkChangeRequest> itList = list.iterator(); while(itList.hasNext()) { Employee emp = itList.next(); getHibernateTemplate().delete(emp); } 

Do they differ significantly in performance? Both of them are essentially the same, i.e. Does the deleteAll method delete one by one?

Would it also be better to do this in SQL using the following query?

 " delete from Employee where name = 'John'" 
+4
source share
2 answers

Yes. It would be better to use SQL or HQL to delete all employee records named "John", rather than the first option. Just because in the first option you need to download the entire employee record from db for deletion. This is obviously an additional task.

I think that for delete() and deleteAll() there will be no difference except delete() , it will create a new session for each call; whereas in deleteAll() all objects will be deleted in one session. But the number of requests will be the same.

+1
source

I suggest you read this section, one shot delete :

https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/performance.html#performance-collections-oneshotdelete

Removing collection items one after another can sometimes be extremely inefficient. Hibernate does not know this in the case of a new empty collection (for example, you called list.clear ()). In this case, Hibernate will produce one DELETE.

+1
source

Source: https://habr.com/ru/post/1399152/


All Articles