Related to issue 27262: Run direct update query to improve performance
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Tue, 02 Jun 2015 14:39:07 +0200
changeset 26858 d73cfed50b6e
parent 26857 89ecff04c23e
child 26859 7eae99f7fe79
Related to issue 27262: Run direct update query to improve performance

Instead of getting all the sales order lines to update the reference line, we have replaced by a direct Update HQL, which improves performance and reduces the memory usage
src/org/openbravo/event/OrderLineEventHandler.java
--- a/src/org/openbravo/event/OrderLineEventHandler.java	Wed May 27 11:59:32 2015 +0530
+++ b/src/org/openbravo/event/OrderLineEventHandler.java	Tue Jun 02 14:39:07 2015 +0200
@@ -21,12 +21,12 @@
 import javax.enterprise.event.Observes;
 
 import org.apache.log4j.Logger;
-import org.hibernate.criterion.Restrictions;
+import org.hibernate.Query;
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.client.kernel.event.EntityDeleteEvent;
 import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
-import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.common.order.OrderLine;
 
@@ -36,25 +36,34 @@
 
   @Override
   protected Entity[] getObservedEntities() {
-    // TODO Auto-generated method stub
     return entities;
   }
 
-  public void onDelete(@Observes EntityDeleteEvent event) {
+  public void onDelete(@Observes
+  EntityDeleteEvent event) {
     if (!isValidEvent(event)) {
       return;
     }
-    // Get cuStrrent orderline
-    OrderLine orderLine = (OrderLine) event.getTargetInstance();
-    OBCriteria<OrderLine> orderLineCriteria = OBDal.getInstance().createCriteria(OrderLine.class);
-    // Get all orderlines which have ref_orderline_id as the current orderline
-    orderLineCriteria.add(Restrictions.eq(OrderLine.PROPERTY_SOPOREFERENCE, orderLine));
-    if (orderLineCriteria.count() > 0) {
-      for (OrderLine orderlines : orderLineCriteria.list()) {
-        // Set ref_orderline_id = null for all such orderlines in the above criteria
-        orderlines.setSOPOReference(null);
-        OBDal.getInstance().save(orderlines);
-      }
+
+    removeSoPoReference(event);
+  }
+
+  private void removeSoPoReference(EntityDeleteEvent event) {
+    try {
+      OBContext.setAdminMode(true);
+      final OrderLine thisLine = (OrderLine) event.getTargetInstance();
+      final StringBuffer hql = new StringBuffer();
+      hql.append(" update from OrderLine ol ");
+      hql.append(" set ol.sOPOReference.id = null ");
+      hql.append(" where ol.sOPOReference.id = :thisLine ");
+      hql.append(" and ol.client.id = :clientId ");
+
+      Query query = OBDal.getInstance().getSession().createQuery(hql.toString());
+      query.setString("thisLine", thisLine.getId());
+      query.setString("clientId", thisLine.getClient().getId());
+      query.executeUpdate();
+    } finally {
+      OBContext.restorePreviousMode();
     }
   }
 }