In our case, the “real” aggregate identifier is generated when the Aggregate is saved in DB using JPA sequence table approach.
public class SomeClass extends AbstractAnnotatedAggregateRoot {
@Id
@TableGenerator(name = “_GEN”, table = “SEQUENCE”, pkColumnName = “SEQ_NAME”, valueColumnName = “SEQ_COUNT”, pkColumnValue = “_SEQ”)
@GeneratedValue(strategy = GenerationType.TABLE, generator = “_GEN”)
private Long id;
public SomeClass(String name) {
apply(new SomeClassCreatedEvent(name));
}
@EventHandler
public void on(SomeClassCreatedEvent event) {
this.name = event.getName();
}
But, Axon expects the identifier to be provided during aggregate construction for its event dispatching to work otherwise I get this exception…
Caused by: org.axonframework.domain.AggregateIdentifierNotInitializedException: AggregateIdentifier is unknown in [x.y.SomeClass]. Make sure the Aggregate Identifier is initialized before registering events.
at org.axonframework.domain.AbstractAggregateRoot.getEventContainer(AbstractAggregateRoot.java:163)
at org.axonframework.domain.AbstractAggregateRoot.registerEvent(AbstractAggregateRoot.java:72)
at org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot.apply(AbstractEventSourcedAggregateRoot.java:96)
at org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot.apply(AbstractEventSourcedAggregateRoot.java:75)
at x.y.SomeClass.(SomeClass.java:88)
I could make the @AggregateIdentifier be the “name” for Axon to be happy but it isn’t really the Id of the class. And I would like both to be the same and not do something special for Axon.
Does Axon provide a way to assign the target identifier post DB commit or it is required that the aggregate identifier be known upfront for this to work?
Regards,
Aditya