EntityListener is not updating

I am having a strange behaviour in EntityListener recently. It is not updating a secondary Entity even I see through debugging that program is executed properly but in the end there is update. Please see below the image:

image

Here is the code:

@Component("erp_MoneyReceiptLineEntityListener")
public class MoneyReceiptLineEntityListener implements BeforeDeleteEntityListener<MoneyReceiptLine>, BeforeInsertEntityListener<MoneyReceiptLine>, BeforeUpdateEntityListener<MoneyReceiptLine> {
    @Inject
    private Persistence persistence;

    @Inject
    private UserSessionSource userSessionSource;

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


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


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


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

    if(entity.getPaidAmount() != null) {

        BusinessIndustry businessIndustry = userSessionSource.getUserSession().getAttribute("businessIndustry");

        //Company company = em.find(Company.class, entity.getMoneyReceipt().getCompany().getId());
        //BusinessIndustryArea businessIndustryArea = company.getBusinessIndustry().getBusinessIndustryArea();
        BusinessIndustryArea businessIndustryArea = businessIndustry.getBusinessIndustryArea();

        if(businessIndustryArea.equals(BusinessIndustryArea.PROPERTY_DEVELOPMENT)) {
        //if(businessIndustry.getBusinessIndustryArea().equals(BusinessIndustryArea.PROPERTY_DEVELOPMENT)) {
            if(entity.getPropertyAllotmentPaymentSchedule() !=null) {
                PropertyAllotmentPaymentSchedule paymentSchedule = entity.getPropertyAllotmentPaymentSchedule();
                if (crudAction.equalsIgnoreCase("create")) {
                    paymentSchedule.setPaidAmount((paymentSchedule.getPaidAmount() == null ? BigDecimal.ZERO : paymentSchedule.getPaidAmount()).add(entity.getPaidAmount()));

                } else if (crudAction.equalsIgnoreCase("delete")) {
                    paymentSchedule.setPaidAmount((paymentSchedule.getPaidAmount() == null ? BigDecimal.ZERO : paymentSchedule.getPaidAmount()).subtract(entity.getPaidAmount()));


                } else if (crudAction.equalsIgnoreCase("update")) {
                    BigDecimal oldValue = (BigDecimal) persistence.getTools().getOldValue(entity, "paidAmount");

                    paymentSchedule.setPaidAmount((paymentSchedule.getPaidAmount() == null ? BigDecimal.ZERO : paymentSchedule.getPaidAmount()).add(entity.getPaidAmount()).subtract(oldValue));

                }

                paymentSchedule.setBalanceAmount(paymentSchedule.getInstallmentAmount().subtract(paymentSchedule.getAdvanceAdjustmentAmount()).subtract(paymentSchedule.getPaidAmount()));

            }
        }else{
            if(entity.getCustomerInvoice() !=null) {

                CustomerInvoice invoice = em.merge(entity.getCustomerInvoice());

                if (crudAction.equalsIgnoreCase("create")) {
                    invoice.setAmountPaid((invoice.getAmountPaid() == null ? BigDecimal.ZERO : invoice.getAmountPaid()).add(entity.getPaidAmount()));
                } else if (crudAction.equalsIgnoreCase("delete")) {
                    invoice.setAmountPaid((invoice.getAmountPaid() == null ? BigDecimal.ZERO : invoice.getAmountPaid()).subtract(entity.getPaidAmount()));
                } else {  //Update
                    BigDecimal oldValue = (BigDecimal) persistence.getTools().getOldValue(entity, "paidAmount");
                    invoice.setAmountPaid((invoice.getAmountPaid() == null ? BigDecimal.ZERO : invoice.getAmountPaid()).add(entity.getPaidAmount()).subtract(oldValue));
                }

            }
        }
    }
}

}

There is no exception that I could follow to fix. I am using platform version 6.10.8. Any thoughts?

Hi,
Most probably paymentSchedule entity is detached in your case. Therefore it will not automatically save changes you made to this entity.

You can write this:

PropertyAllotmentPaymentSchedule paymentSchedule = entity.getPropertyAllotmentPaymentSchedule();

if (PersistenceHelper.isDetached(paymentSchedule)) {
    paymentSchedule = em.reload(paymentSchedule);
}
// make the changes
1 Like

Yes that was the reason here. Thanks.

But wondering what happened in another but similar case below. As you see in line 35, why I am getting this message…
and used same same code for reloading in case it is detached but no good result!

image

resolved the issue.