public void update(Order order, Admin operator) { Assert.notNull(order); Order pOrder = orderDao.find(order.getId()); if (pOrder.getIsAllocatedStock()) { for (OrderItem orderItem : pOrder.getOrderItems()) { if (orderItem != null) { Product product = orderItem.getProduct(); productDao.lock(product, LockModeType.PESSIMISTIC_WRITE); if (product != null && product.getStock() != null) { product.setAllocatedStock( product.getAllocatedStock() - (orderItem.getQuantity() - orderItem.getShippedQuantity())); productDao.merge(product); orderDao.flush(); staticService.build(product); } } } for (OrderItem orderItem : order.getOrderItems()) { if (orderItem != null) { Product product = orderItem.getProduct(); productDao.lock(product, LockModeType.PESSIMISTIC_WRITE); if (product != null && product.getStock() != null) { product.setAllocatedStock( product.getAllocatedStock() + (orderItem.getQuantity() - orderItem.getShippedQuantity())); productDao.merge(product); productDao.flush(); staticService.build(product); } } } } orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.modify); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }