Selectively applying events on entities


One of my aggregates contains multiple entities of the same class. Most events apply only to one of these entities, so I end up doing something like this:

public void handle(final Event event) {
if (event.getEntityId().equals(id)) {


It seems very unnatural. Is there a better way to do this ?

Hi Nicolas,

at this moment, that’s the only way to deal with it.
An improvement for this has been on my wishlist for a while. I’m open to suggestions.



If your entity follows the same pattern repeatedly, and assuming your events have a common superclass, you can factor this out into:

public void handleRecursively(DomainEventMessage event) {
    if (!MyChildEntityEvent.class.isAssignableFrom(event.getPayloadType())
            >> ((MyChildEntityEvent) event.getPayload()).getEntityId().equals( {

This should definitely work, although be careful with the serialized form of this event. It is a bad idea to have a property “entityId” in the serialized event. Always make sure you use a property name with functional meaning, such as “orderLineId”.

Yes, of course “MyChildEntityEvent” is a pretty bad name too :wink: