AggregateNotFoundException

Hi!
I have a simple code with axonframework, but couldn’t make it working because of AggregateNotFoundException that occurs when I’m trying to call the method from commandGateway.
I have a working example with Reservation and now I want to add several Reservation to one ReservationGroup, but my new code doesn’t work.
My simplified code is:

`
@Component
public class ReservationCommandService {

private final ReservationCommandGateway commandGateway;
private final ReservationGroupCommandGateway reservationGroupCommandGateway;

@Autowired
public ReservationCommandService(ReservationCommandGateway commandGateway, ReservationGroupCommandGateway reservationGroupCommandGateway) {
this.commandGateway = commandGateway;
this.reservationGroupCommandGateway = reservationGroupCommandGateway;
}

//works correct
public ReservationId startReservation(…) {
ReservationId reservationIdentifier = new ReservationId(); //UUID.randomUUID().toString()

StartReservation command = new StartReservation(reservationIdentifier, …);
commandGateway.startReservation(command); //ok
return reservationIdentifier;
}

//doesn’t work
public ReservationGroupId startReservationGroup(String tenant, String username, StartReservationGroupDTO startReservationGroupDTO) {
ReservationGroupId reservationGroupId = new ReservationGroupId(); //UUID.randomUUID().toString()

List reservationIdList = new ArrayList<>();

for (int i = 0; i < 10; ++i) {
ReservationId reservationId = startReservation(…);
reservationIdList.add(reservationId);
}

SaveReservationGroupCommand saveReservationGroupCommand = new SaveReservationGroupCommand(reservationGroupId, reservationIdList);

// the problem is in the next line
// org.axonframework.commandhandling.model.AggregateNotFoundException: The aggregate was not found in the event store
reservationGroupCommandGateway.saveReservationGroup(saveReservationGroupCommand);

return reservationGroupId;
}

}

@Configuration
public class AxonConfig {

@Bean
public MongoEventStorageEngine storageEngine(Serializer serializer,MongoTemplate mongoTemplate) {
return new MongoEventStorageEngine(serializer,null,mongoTemplate,new DocumentPerEventStorageStrategy());
}
@Bean
public ReservationCommandGateway commandReservationService(CommandGatewayFactory commandGatewayFactory) {
return commandGatewayFactory.createGateway(ReservationCommandGateway.class);
}
@Bean
public ReservationGroupCommandGateway commandReservationGroupService(CommandGatewayFactory commandGatewayFactory) {
return commandGatewayFactory.createGateway(ReservationGroupCommandGateway.class);
}

}

public interface ReservationGroupCommandGateway {
void saveReservationGroup(SaveReservationGroupCommand command);
}

@Aggregate
@NoArgsConstructor
public class ReservationGroup {
@AggregateIdentifier
private ReservationGroupId reservationGroupId;

@EventSourcingHandler
void on(ReservationGroupSavedEvent event) {
this.reservationGroupId = event.getReservationGroupId();
}

@CommandHandler
public void handle(SaveReservationGroupCommand command) {
apply(new ReservationGroupSavedEvent(command.getReservationGroupId(), command.getReservationIdList()));
}

}

@Component
public class ReservationGroupEventListener {
private final ReservationGroupRepository repository;

@Autowired
public ReservationGroupEventListener(ReservationGroupRepository repository) {
this.repository = repository;
}

@EventHandler
public void on(ReservationGroupSavedEvent event) {
ReservationGroupState reservationGroupState = new ReservationGroupState();
reservationGroupState.setReservationGroupId(event.getReservationGroupId());

repository.save(reservationGroupState);
}
}

@RepositoryDefinition(domainClass = ReservationGroupState.class, idClass = ReservationGroupId.class)
public interface ReservationGroupRepository {
void save(ReservationGroupState reservationGroupState);
ReservationGroupState findOne(ReservationGroupId reservationGroupId);
}

`

I really appreciate any help.

Hi Sergey,

I don’t see any command handlers on your ReservationGroup aggregate that allow the creation of a new instance. I would expect one of the @CommandHandler methods to be on a constructor, which would indicate that that specific command will create a new instance. Note that you’d also need to explicitly define a default (no-arg) constructor as well.

If you command handlers are instance methods, Axon will attempt to load the instance using the value of the @TargetAggregateIdentifier annotated field/method in the command message.

Hope this helps.
Cheers,

Allard