【发布时间】:2020-11-27 07:28:04
【问题描述】:
环境设置:Axon 4.4,H2Database(我们正在做组件测试作为 CI 的一部分) 代码看起来像这样。
@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
}
}
我正在使用状态存储聚合,但在嵌入 H2 的 Spring 测试期间,我不断收到错误消息。非嵌入式模式下的 PGSQL DB 不会出现同样的问题,但我们无法在管道中运行它。
错误:“java.lang.IllegalStateException:尚未设置聚合标识符。它必须最迟在应用创建事件时设置”
我通过了 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);
}
这个序列被设置为 2,因此它抛出异常而不是懒惰地初始化聚合
对此有什么解决办法?我是否缺少某些配置或需要修复 Axon 代码?
【问题讨论】:
-
您介意补充一下您是如何使用 Axon 的测试夹具来测试您的聚合体的吗?期望在非弹簧式测试夹具中自动连接所有内容可能是罪魁祸首。