Prior state of entity in listener

I am just wondering if within the listener I can get the previous state of the entity and compare it to the new state of the entity.

Kind of like how you can in a React framework.

Something like:

 @Override
    public void onBeforeUpdate(Entity existingState, Entity newState, EntityManager entityManager) {
        if(previousState.someAttribute <> newState.someAttribute){
            thenDoSomething()
}

I presume you would need to do some sort of native database query?

Or maybe would

Entity oldEntity = entityManager.find(Entity.class, newState.id)

load the old state of the entity from the database? Or would that just copy the current new state?

Or is there another solution?

Hi @daryn,
You can try using PersistenceTools.

@Inject
private Persistence persistence;

persistence.getTools().getOldValue(entity, “field”);

Ok great,

I’ll give that a try.

Thanks!

Hi,

the ItemPropertyChangeEvent contains the previous value, so the following will work for you:

@Subscribe(id = "visitDc", target = Target.DATA_CONTAINER)
public void onVisitDcItemPropertyChange(InstanceContainer.ItemPropertyChangeEvent<Visit> event) {
    Object prevValue = event.getPrevValue();
    ...
}

Gleb