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.