How to use Update event

In Jmix, I see we have “isNew”, “isDeleted” events, what is equivalent to “isUpdated”?

image

There is no such state.

Ok, that means we can use if(!entityStates.usNew(entity) && !entityStates.usDeleted(entity))
to get a modified state, do you think that should work technically?

Probably not, since if both of those are false it could also be that the entity has not been modified.

@krivopustov What is the suggested way to check if an entity is modified, and get the new/old values of its properties? This is a very common business need.

1 Like

There is explicit API for it in EntityChangedEvent.
In other cases the only way to get new/old values is to load the same instance using DataManager and compare attributes.

Thank you Konstantin. This is now more clear.

I have just written a few lines of code accordingly in my application that is appened below:

  @EventListener
    public void onMemberLoanChangedBeforeCommit(EntityChangedEvent<MemberLoan> event) {
        MemberLoan memberLoan = dataManager.load(MemberLoan.class).id(event.getEntityId()).one();
        if(event.getType().equals(EntityChangedEvent.Type.UPDATED) 
                && event.getChanges().isChanged("approved") 
                && memberLoan.getApproved()==true 
                && memberLoan.getApInvoice()==null){
            ApInvoice apInvoice = this.generateApInvoice(memberLoan);
            memberLoan.setApInvoice(apInvoice);
        }
    }

MemberLoan memberLoan = dataManager.load(MemberLoan.class).id(event.getEntityId()).one();
By this, I am capturing the entity with current values, whereas the OldValue can be derived from the event.

and subsequent lines are self-explanatory and ultimately based on the change, ApInvoice is created and the current entity is updated with the ApInvoice entity.