Why do my Entity Listener is inserting amount multiple of number of lines?

I have two entities : Journal and JournalLine (composite). In JournalLine If I have 3 lines of record with say $1000, $200 and $500, after i save the entry, the Entity Listener save the record in Another Entity with the following values: $3000, $600 and $1500. I have gone through my codes several times but I might be missing something… something is wrong, would appreciate if anyone can find what went wrong:

here is my Listener:


@Inject
private Persistence persistence;

@Inject
private Metadata metadata;

@Override
public void onBeforeInsert(JournalLine entity, EntityManager entityManager) {

    updateInsertBalance(entity, entityManager, 1);
}

@Override
public void onBeforeDelete(JournalLine entity, EntityManager entityManager) {
    updateInsertBalance(entity, entityManager, 3);
}

@Override
public void onBeforeUpdate(JournalLine entity, EntityManager entityManager) {
   updateInsertBalance(entity, entityManager, 2);
}

private void updateInsertBalance(JournalLine operation, EntityManager entityManager, int crudAction) {
    TypedQuery<AccountsBalance> query = entityManager.createQuery("select e from erp$AccountsBalance e " +
            "where e.account.id = ?1 AND e.company.id = ?2 AND e.financialYear.id = ?3 AND e.financialPeriod.id = ?4", AccountsBalance.class);
        query.setParameter(1, operation.getAccount().getId())
            .setParameter(2, operation.getJournal().getCompany().getId())
            .setParameter(3, operation.getJournal().getFinancialYear().getId())
            .setParameter(4, operation.getJournal().getFinancialPeriod().getId())
            .setView(AccountsBalance.class, "accountsBalance-view");

    AccountsBalance list = query.getFirstResult();

    BigDecimal oldDrAmt = BigDecimal.ZERO;
    BigDecimal oldCrAmt = BigDecimal.ZERO;
    BigDecimal netAmt = BigDecimal.ZERO;

    if (list==null) {
        BigDecimal a = new BigDecimal(0);
        AccountsBalance balance = metadata.create(AccountsBalance.class);
        balance.setCompany(operation.getJournal().getCompany());
        balance.setFinancialYear(operation.getJournal().getFinancialYear());
        balance.setFinancialPeriod(operation.getJournal().getFinancialPeriod());
        balance.setAccount(operation.getAccount());
        balance.setAccountSub(operation.getAccountSub());

        balance.setCostCentre(operation.getCostCentre());
        if (operation.getDebit().doubleValue()>0){
            balance.setAmount(operation.getDebit());
        } else {
            balance.setAmount(a.subtract(operation.getCredit()));
        }
        entityManager.persist(balance);
    } else {
        BigDecimal a=new BigDecimal(0);
        if(list.getAmount() != null){
            a= list.getAmount();
        }
        if(crudAction==2) {
            oldDrAmt = (BigDecimal) persistence.getTools().getOldValue(operation, "debit");
            oldCrAmt = (BigDecimal) persistence.getTools().getOldValue(operation, "credit");
            if (oldDrAmt == null)
                oldDrAmt = new BigDecimal(0);
            if (oldCrAmt == null)
                oldCrAmt = new BigDecimal(0);
            netAmt = a.subtract(oldDrAmt).add(oldCrAmt);
            if(operation.getDebit().doubleValue()>0){
                list.setAmount(netAmt.add(operation.getDebit()));
            }else{
                list.setAmount(netAmt.subtract(operation.getCredit()));
            }
        } else {  //delete
            if(operation.getDebit().doubleValue()>0){
                list.setAmount(a.subtract(operation.getDebit()));
            }else{
                list.setAmount(a.add(operation.getCredit()));
            }
        }
    }
}

Hi Mortoza,

Probably, the reason of the problem is that you make changes in “Before”- listeners. It means that new values are not applied to JournalLine at the moment the method is called. Try to call updateInsertBalance in “After”-listeners.