Environment setup : Axon 4.4, H2Database( we are doing component testing as part of the CI) Code looks something like this.
@Aggregate(repository = "ARepository")
@Entity
@DynamicUpdate
@Table(name = "A")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@Log4j2
Class A implements Serializable {
@CommandHandler
public void handle(final Command1 c1) {
apply(EventBuilder.buildEvent(c1));
}
@EventSourcingHandler
public void on(final Event1 e1) {
//some updates to the modela
apply(new Event2());
}
@Id
@AggregateIdentifier
@EntityId
@Column(name = "id", length = 40, nullable = false)
private String id;
@OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
orphanRemoval = true,
targetEntity = B.class,
mappedBy = "id")
@AggregateMember(eventForwardingMode = ForwardMatchingInstances.class)
@JsonIgnoreProperties("id")
private List<C> transactions = new ArrayList<>();
}
@Entity
@Table(name = "B")
@DynamicUpdate
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@Log4j2
Class B implements Serializable {
@Id
@EntityId
@Column(name = "id", nullable = false)
@AggregateIdentifier
private String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "id", referencedColumnName = "id")})
@JsonIgnoreProperties("transactions")
private A a;
@EventSourcingHandler
public void on(final Event2 e2) {
//some updates to the model
}
}
I'm using a state store aggregate but I keep getting the error randomly during Spring Test with embedded H2. The same issue does not occur with a PGSQL DB in non embedded mode but than we are not capable of runnign it in the pipeline.
Error : "java.lang.IllegalStateException: The aggregate identifier has not been set. It must be set at the latest when applying the creation event"
I stepped through AnnotatedAggregate
protected <P> EventMessage<P> createMessage(P payload, MetaData metaData) {
if (lastKnownSequence != null) {
String type = inspector.declaredType(rootType())
.orElse(rootType().getSimpleName());
long seq = lastKnownSequence + 1;
String id = identifierAsString();
if (id == null) {
Assert.state(seq == 0,
() -> "The aggregate identifier has not been set. It must be set at the latest when applying the creation event");
return new LazyIdentifierDomainEventMessage<>(type, seq, payload, metaData);
}
return new GenericDomainEventMessage<>(type, identifierAsString(), seq, payload, metaData);
}
return new GenericEventMessage<>(payload, metaData);
}
The sequence for this gets set to 2 and hence it throws the exception instead of lazily initializing the aggregate
Whats the fix for this? Am i missing some configuration or needs a fix in Axon code?
Finally after debugging we found out that in class B we were not setting the id for update event
Once we did that the issue went away.