IncompatibleAggregateException : Aggregate identifier must be non-null after applying an event

Hi ,

Update command fails after successful Create command with error – “Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.” .

This happens both at runtime and during tests. Can somebody please explain what is the problem here?

Runtime :

2019-02-14 15:52:14.824 DEBUG [ XNIO-2 task-1] o.a.c.model.LockingRepository : Exception occurred while trying to load an aggregate. Releasing lock.

org.axonframework.eventsourcing.IncompatibleAggregateException: Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.
at org.axonframework.eventsourcing.EventSourcedAggregate.publish(EventSourcedAggregate.java:259)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at org.axonframework.eventsourcing.EventSourcedAggregate.lambda$initializeState$0(EventSourcedAggregate.java:287)
at org.axonframework.commandhandling.model.inspection.AnnotatedAggregate.lambda$execute$2(AnnotatedAggregate.java:353)
at org.axonframework.commandhandling.model.AggregateLifecycle.lambda$execute$2(AggregateLifecycle.java:204)
at org.axonframework.messaging.Scope.executeWithResult(Scope.java:99)
at org.axonframework.commandhandling.model.AggregateLifecycle.execute(AggregateLifecycle.java:203)
at org.axonframework.commandhandling.model.inspection.AnnotatedAggregate.execute(AnnotatedAggregate.java:353)
at org.axonframework.eventsourcing.EventSourcedAggregate.initializeState(EventSourcedAggregate.java:284)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:387)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:48)
at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:195)
at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:50)
at org.axonframework.commandhandling.model.AbstractRepository.lambda$load$11(AbstractRepository.java:151)
at java.util.HashMap.computeIfAbsent(HashMap.java:1126)
at org.axonframework.commandhandling.model.AbstractRepository.load(AbstractRepository.java:150)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:219)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:213)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:175)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:44)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
at org.axonframework.messaging.interceptors.CorrelationDataInterceptor.handle(CorrelationDataInterceptor.java:65)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:55)
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
at org.axonframework.commandhandling.SimpleCommandBus.handle(SimpleCommandBus.java:148)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:121)
at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:85)
at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(AbstractCommandGateway.java:79)
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:95)
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.sendAndWait(DefaultCommandGateway.java:113)
at com…CredibilityService.sendUpdateCredibilityCommand(CredibilityService.java:115)

@Test
public void testUpdateCommand() {
    fixtureConfiguration.given(new AddAccountCommand("23525", "CURRENT", LocalDate.now(),LocalDate.now()))
                        .when(new UpdateAccountCommand("23525", "PASTDUE", LocalDate.now(),LocalDate.now()))
                        .expectEvents(new AccountUpdateEvent("23525", "PASTDUE", LocalDate.now(), LocalDate.now()));
}

RESULTS :

org.axonframework.test.AxonAssertionError: The published events do not match the expected events

Expected Actual
com…AccountUpdateEvent < >

A probable cause for the wrong chain of events is an exception that occurred while handling the command.
org.axonframework.eventsourcing.IncompatibleAggregateException: Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.
at org.axonframework.eventsourcing.EventSourcedAggregate.publish(EventSourcedAggregate.java:259)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at org.axonframework.eventsourcing.EventSourcedAggregate.lambda$initializeState$0(EventSourcedAggregate.java:287)
at org.axonframework.commandhandling.model.inspection.AnnotatedAggregate.lambda$execute$2(AnnotatedAggregate.java:353)
at org.axonframework.commandhandling.model.AggregateLifecycle.lambda$execute$2(AggregateLifecycle.java:204)
at org.axonframework.messaging.Scope.executeWithResult(Scope.java:99)
at org.axonframework.commandhandling.model.AggregateLifecycle.execute(AggregateLifecycle.java:203)
at org.axonframework.commandhandling.model.inspection.AnnotatedAggregate.execute(AnnotatedAggregate.java:353)
at org.axonframework.eventsourcing.EventSourcedAggregate.initializeState(EventSourcedAggregate.java:284)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:387)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:48)
at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:195)
at org.axonframework.commandhandling.model.LockingRepository.doLoad(LockingRepository.java:50)
at org.axonframework.commandhandling.model.AbstractRepository.lambda$load$11(AbstractRepository.java:151)
at java.util.HashMap.computeIfAbsent(HashMap.java:1126)
at org.axonframework.commandhandling.model.AbstractRepository.load(AbstractRepository.java:150)
at org.axonframework.test.aggregate.AggregateTestFixture$IdentifierValidatingRepository.load(AggregateTestFixture.java:602)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:219)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler$AggregateCommandHandler.handle(AggregateAnnotationCommandHandler.java:213)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:175)
at org.axonframework.commandhandling.AggregateAnnotationCommandHandler.handle(AggregateAnnotationCommandHandler.java:44)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:57)
at org.axonframework.test.aggregate.AggregateTestFixture$AggregateRegisteringInterceptor.handle(AggregateTestFixture.java:815)
at org.axonframework.messaging.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:55)
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:69)
at org.axonframework.commandhandling.SimpleCommandBus.handle(SimpleCommandBus.java:148)
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:121)
at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:85)
at org.axonframework.test.aggregate.AggregateTestFixture.when(AggregateTestFixture.java:372)
at org.axonframework.test.aggregate.AggregateTestFixture.when(AggregateTestFixture.java:359)

Hello Shivashankar,

I think the issue is in:

fixtureConfiguration.given(new AddAccountCommand("23525", "CURRENT", LocalDate.now(),LocalDate.now()))

Try:

fixtureConfiguration.given(new **AccountAddedEvent**("23525", "CURRENT", LocalDate.now(),LocalDate.now()))

Best,
Ivan

Hello Ivan,

Thank you . The test case works fine after the change.

However the issue is seen(Exception stacktrace provided in earlier post) during runtime while executing the flow. AddAccount Command/event works fine . Update to AccountAggregate with UpdateCommand fails later.

The update command is not reaching the Aggregate command handler .I noticed ,when i put a break point, root aggregate is having null values . in org.axonframework.eventsourcing.EventSourcedAggregate --> initializeState(DomainEventStream eventStream)
and resulting in error “Aggregate identifier must be non-null after applying an event”

Please help.

Hi,

query your event table for the aggregate and check, if the events were serialized the right way.
It could be a result of an serialization error, that occurred much sooner.
Also check your log when creating and changing the events.
It also helps to write unit tests for events and assert their serializability.

Best regards. J.

Hi Shivashankar,

Johnny has a fair suggestion for you to look at.

Another thing to check in your sample is that your handling the AccountAddedEvent, assuming that that’s the first event published from your Aggregate.

There is a hard requirement to handle the ‘aggregate creation event’ to set the aggregate identifier.
Other, Axon will not know how to source that aggregate from the repository to handle subsequent commands; it needs to know for which aggregate a given command is, based on that aggregate identifier.
The first part in the reference guide about the Aggregate should explain this necessity too.

More specifically, bullet point 4 states this requirement.

Hope this helps!

Cheers,
Steven

Hi Steven, J,

I noticed below error in org.axonframework.eventsourcing.EventSourcedAggregate when i tried debugging .

Method threw ‘com.thoughtworks.xstream.converters.ConversionException’ exception. Cannot evaluate org.axonframework.eventsourcing.GenericDomainEventMessage.toString()

Based on your suggestion i tried to narrow down the issue related to serialization and eventually figured out the issue was due to local library.
I removed the local library from pom and the Add/Update events worked fine.

Thank you .

Thanks,
Shiv