[Axon 3] Event wrongly applied to all aggregate members

Hi,

I’m facing an issue with Axon 3 in the context of a “complex” aggregate structure.

Here is the structure of my aggregate :

`
@Aggregate
public class AggregateRoot {

@AggregateIdentifier
private String aggregateRootId;

@AggregateMember
private EntityA entityA;

}

//This entity shares the same identity than the AggregateRoot
public class EntityA {

private String aggregateRootId;
@AggregateMember
private Map<String, EntityB> children = new HashSet<>();

}

//This entity has its own identity
public class EntityB{

private boolean done;

@EntityId
private String entityBId;

@CommandHandler
public void handle(DoSomethingOnMyEntityBCommand command){
apply(new SomethingDoneOnMyEntityBEvent());
}

@EntitySourcingHandler
private on(SomethingDoneOnMyEntityBEvent event){
this.done = true;
}

public class DoSomethingOnMyEntityBCommand {

@TargetAggregateIdentifier
private String aggregateRootId;
//To route the command to the right instance
private entityBId;

}

`

Ok so now, the DoSomethingOnMyEntityBCommand is correctly routed to the right instance (and only this one), but what happens after is quite odd, as the corresponding event is applied to ALL EntityB instances in the aggregate members map.
Do I miss something ? How Axon exactly handles this case ?

The workaround is to move the @EventSourcingHandler in the parent EntityA and change the state from there for the right instance but I wonder if there is a proper way to handle it directly in the EntityB.

Thank you for your help,
Best Regards,
Jerome

Hi Jerome,

the behavior you’re seeing is actually by design. Events are applied to the aggregate as a whole. That means all entities in the aggregate are capable of updating whatever state they need to update when this event occurs. In entities, you should therefore make sure that the event is about the actual instance when you’re updating state.

In upcoming versions, there will be a setting on the @AggregateMember annotation to override this behavior.

Allard

Hi Allard,

Thank you for your quick reply !

Best Regards,
Jerome