Call "a calc" function in parent from ChildEntityListener onAfterUpdate

Hi all,

I have a lot of troubles with this and I decided to ask here.
Right now, I want to simultate the tipical trigger on database. I have an Entity A, and it has a collection of Entity B.

From another entity, I create a new Entity A, and several Entity B attached to entity A. After some troubles I finally got it working as expected commiting in the middle of the process (I would really like to know how in screen works without commiting, but this is another question.
Again, Now, I have an Entity A with a collection of Entity B, and Entity B has a EntityListener afterInsert to call a parent function to recalc:

entity.getEntityA().calcTotals();

Under Entity A calcTotals() I have:

for (Iterator iterator = this.getEntityBCollection().iterator(); iterator.hasNext();){

but getEntityBCollection always return null in this point.

Same code works as expected If I go to the screen, create a new Entity A, and create an EntityB for it.

Whats happening and wich is the correct way to work around that?

Hi Giuseppe,

Use BeforeUpdateEntityListener, it will allow you to traverse the object graph and modify any attribute.

Alternatively, you can use EntityChangedEvent which is sent when an entity is changed in the database. Then you can load related entities and change them, either in the same or in a separate transaction. See an example in the Decouple Business Logic with Application Events guide.

1 Like