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.

Hi!

I faced with the issue and I need advice.
I have :

@Aggregate
class UpdatePermission() {
@AggregateIdentifier
private var id: AxxalonIdentifier? = null

@CommandHandler
fun updatePermission(command: UpdatePermissionCommand) {
var a = AggregateLifecycle.apply(PermissionUpdateEvent(command.id, command.title))
}

@CommandHandler
constructor(command: CreatePermissionCommand) : this() {
var a = AggregateLifecycle.apply(PermissionCreatedEvent(command.id, command.title))

}
@EventSourcingHandler(payloadType = PermissionCreatedEvent::class)
fun on(event: PermissionCreatedEvent) {
this.id = event.id
}
}

Steps for reproduce the exception:

  1. Start server with application
  2. Send ‘Create’ event
  3. Restart server with application
  4. Send ‘Update’ event

Question is: How I can init field which annotated AggregateIdentifier after restart server ? Can I do it at method ‘updatePermission’ ?

Hi Sergey,

Code wise, this approach seems fine to me.
You have a Command Handler Constructor, publishing the PermissionCreatedEvent.
You then use this events in an EventSourcingHandler which sets the id field of the UpdatePermission Aggregate, which is as intended annotated with @AggregateIdentifier.

Granted, you are seeing problems with this set up, so something’s amiss.
Have you verified that your EventSourcingHandler annotated method is being called after the PermissionCreatedEvent has been published?
Have you written Tests to check whether the Aggregate is working as expected? I’d suggest to use Test Fixtures for this by the way, which are provided by the framework.

And lastly, you state that after restarting your server and issuing a different command, you get the problem.
Are you certain your events are persisted somewhere?
If you loose your events, that also means the framework is incapable of Event Sourcing a specific Aggregate; without the events, that’s by definition not possible.

Hope this helps you out a Sergey!

Cheers,
Steven