public class IdentityAgg extends AbstractAnnotatedAggregateRoot implements Serializable {
private static final long serialVersionUID = 8832894803205334145L;
@AggregateIdentifier
private String id;
private String type;
private String name;
private List<Contact> contactList;
public IdentityAgg() {}
public IdentityAgg(String id, String type, String name) {
apply(new IdentityCreatedEvent(id, type, name));
}
@EventSourcingHandler
public void handle(IdentityCreatedEvent event) {
this.id = event.getIdentityId();
this.type = event.getType();
this.name = event.getName();
}
@Component
public class ContactCommandHandler {
@Autowired
private Repository repository;
@CommandHandler
public void handle(RegisterNewContactCommand registerNewContactCommand) throws OPNSException {
IdentityAgg identityAggToAddWithin = (IdentityAgg) repository.load(registerNewContactCommand.getIdentityId()); // this line is throwing the exception
identityAggToAddWithin.addContact(registerNewContactCommand.getIdentityId(), registerNewContactCommand.getContact());
}
@Component
public class IdentityCommandHandler {
@Autowired
private Repository<IdentityAgg> repository;
@CommandHandler
public void handle(CreateIdentityCommand createIdentityCommand {
IdentityAgg identityAgg =
new IdentityAgg(createIdentityCommand.getIdentityId(),createIdentityCommand.getType(),createIdentityCommand.getName());
repository.add(identityAgg);
}
}
Creating new Aggregate Instances When the
@CommandHandler
annotation is placed on an Aggregate's constructor, the respective command will create a new instance of that aggregate and add it to the repository. Those commands do not require to target a specific aggregate instance. That wouldn't make sense, since the instance is yet to be created. Therefore, those commands do not require any@TargetAggregateIdentifier
or@TargetAggregateVersion
annotations, nor will a customCommandTargetResolver
be invoked for these commands.
commandGateway.send(new CreateIdentityCommand(uuid,
identityTypeAndName.getType(), identityTypeAndName.getName()));
@CommandHandler
public IdentityAgg(CreateIdentityCommand createIdentityCommand) {
apply(new IdentityCreatedEvent(createIdentityCommand.getIdentityId(), createIdentityCommand.getType(), createIdentityCommand.getName()));
}
@EventSourcingHandler
public void handle(IdentityCreatedEvent event) {
this.id = event.getIdentityId();
this.type = event.getType();
this.name = event.getName();
}
public class CreateIdentityCommand {
private final String identityId;
private final String type;
private final String name;
public CreateIdentityCommand(String identityId, String type, String name) {
this.identityId = identityId;
this.type = type;
this.name = name;
}
public String getIdentityId() {
return identityId;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
}
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
@Override
protected void publish(EventMessage<?> msg) {
if (msg instanceof DomainEventMessage) {
lastEventSequenceNumber = ((DomainEventMessage) msg).getSequenceNumber();
}
snapshotTrigger.eventHandled(msg);
super.publish(msg);
if (identifierAsString() == null) {
throw new 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.");
}
}
--