getOldValue seems not working

Hi
I have used the following codes in Entity Listener but seems it is not working.


@Component("erp_CustomerInvoiceEntityListener")
public class CustomerInvoiceEntityListener implements BeforeDeleteEntityListener<CustomerInvoice>, BeforeInsertEntityListener<CustomerInvoice>, BeforeUpdateEntityListener<CustomerInvoice> {
    @Inject
    private Metadata metadata;
    @Inject
    private Persistence persistence;

    @Override
    public void onBeforeDelete(CustomerInvoice entity, EntityManager entityManager) {
        updateCRUD(entity, entityManager, "delete");
    }

    @Override
    public void onBeforeInsert(CustomerInvoice entity, EntityManager entityManager) {
        updateCRUD(entity, entityManager, "create");
    }

    @Override
    public void onBeforeUpdate(CustomerInvoice entity, EntityManager entityManager) {
        updateCRUD(entity, entityManager, "update");
    }

    private void updateCRUD(CustomerInvoice entity, EntityManager em, String crudAction) {

        if(entity.getAmountTotal() != null) {
            BigDecimal oldValue = BigDecimal.ZERO;
            if (crudAction.equalsIgnoreCase("update")) {
                oldValue = (BigDecimal) persistence.getTools().getOldValue(entity, "amountTotal");
                if (oldValue == null) {
                    oldValue = new BigDecimal(0);
                }
            }

            TypedQuery<CustomerProfile> query = em.createQuery("select e from erp$CustomerProfile e " +
                    "where e.id = ?1", CustomerProfile.class);
            query.setParameter(1, entity.getCustomerProfile().getId());
            CustomerProfile customerProfile = query.getFirstResult();

            if (customerProfile != null) {
                if (crudAction.equalsIgnoreCase("create")) {
                    customerProfile.setCurrentBalanceAmount(customerProfile.getCurrentBalanceAmount().add(entity.getAmountTotal()));

                } else if (crudAction.equalsIgnoreCase("update")) {

                    if (customerProfile.getCurrentBalanceAmount() == null)
                        customerProfile.setCurrentBalanceAmount(BigDecimal.ZERO);

                    customerProfile.setCurrentBalanceAmount(customerProfile.getCurrentBalanceAmount().add(entity.getAmountTotal()).subtract(oldValue));

                } else if (crudAction.equalsIgnoreCase("delete")) {

                    if (customerProfile.getCurrentBalanceAmount() == null)
                        customerProfile.setCurrentBalanceAmount(BigDecimal.ZERO);

                    customerProfile.setCurrentBalanceAmount(customerProfile.getCurrentBalanceAmount().subtract(entity.getAmountTotal()));
                }
            }
        }
    }
}

What I observed that even if I open CustomerInvoiceEditor and save without any change, the field CurrentBalanceAmount of Entity CustomerProfile is incremented by the AmountTotal field value in instead of no change as old and new value are the same. Whatever is the current value in AmountTotal field of Entity CustomerInvoice is added. Is there anything mistake here or it’s a bug?

I noticed my old post that was solved but seems it’s now not working for me!
https://www.cuba-platform.com/discuss/t/beforeupdateentitylistener-how-to-get-the-old-new-value-before-update

Hi Mortoza,

We cannot reproduce the issue.
Also, I don’t see any apparent mistakes in your code. Try to debug step-by-step or add some log output to see actual values.