CommandHandlingMember / EventSourcingMember and HybridJpaRepository


When using the HybridJpaRepository the aggregate root is not registered with the command handling entity prior to command handler invocation which causes an exception when performing an apply inside the entity. When eventsourcing this relationship is re-established since my test cases using fixtures pass, however when the application is running and using the Jpa store to rehydrate the aggregate it is not.

I was able to work around the issue by using a @PostConstruct method on the root to iterate over the child entities and register the root. However this is not ideal since any lazy associations are loaded whether they will handle the command or not.

I think the proper place to do this is in AggregateCommandHandlerInspector.invoke(Object target, Message message) just prior to invocation; what do you think ?

This brings me to EventSourcingMembers prior to propagating events the code iterates recursively over all the child entities to register the aggregate root whether they will handle the event or not. This will trigger initialization of any of the lazy associations irrespective of whether they will handle the event or not ( similar issue to the workaround i have put in for the command handling ). Should it not be possible to register the aggregate root only in the case when the entity will handle a particular event ?

thank you

Hi Igor,

you’re right. The invocation of the command on an aggregate is the right moment to ensure the aggregate root is known to all the entities in the aggregate. The problem with doing it lazily is that you can never tell which entities will apply an event. It is very well possible that a command spans multiple entities, that each want to apply an event. Therefore, the entire aggregate needs to be aware of the root.