Command Handling in Entity

I have an aggregate which has a 1 to many relationship with an entity. For this I have a collection attribute in my aggregate annotated with @org.axonframework.modelling.command.AggregateMember(type=MyEntity.class). I have command handler methods in the entity. I have gone through this https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/multi-entity-aggregates but it is not clear to me how the routing of the command will happen to the entity instance. How would axon identify the entity instance to route the command?

Is there any example of how the commands can be routed to the entity class? I am not seeing the entity class shown in documentation in the gift card demo source.

Regards
Sunil

Hi Sunil,

Axon routes commands to members inside an aggregate based on routing keys. Each Entity’s identifier is used as the routing key. Axon expects that identifier to be available on the command itself. It will look for a property that matches the routing key on the command handler’s annotation. If that isn’t available, it will check the routing key defined on the entities @EntityId annotation. If that is also not available, it will use the field name of the @EntityId annotated field as the routing key.
An entity instance matches if its identifier is equal to the value of the property on the command.

There is a more elaborate sample project that routes commands to specific entities. Specifically, these classes:
https://github.com/pivotalsoftware/ESarch/tree/master/trading-engine/src/main/java/io/pivotal/refarch/cqrs/trader/tradingengine/trade

You can find the details of the entire demo application here: https://axontrader.cfapps.io/

Kind regards,