Skip to content
Advertisement

aggregate not found in the event store

I am trying to add data using CQRS framework AXON. But while hitting the API(used to add an order). I am getting the below error:-

Command 'com.cqrs.order.commands.CreateOrderCommand' resulted in org.axonframework.modelling.command.AggregateNotFoundException(The aggregate was not found in the event store)

But i already have an Aggregate in my code(OrderAggregate.Java).

The Full code can be found at – https://github.com/iftekharkhan09/OrderManagementSystem

API to add Order – http://localhost:8080/confirmOrder

Request Body:-

{
    "studentName":"Sunny Khan"
}

Can anyone please tell me where am i doing wrong? Any help is appreciated!

Advertisement

Answer

For other readers, let me share the Aggregate you’ve created in your repository:

@Aggregate
public class OrderAggregate {
    public OrderAggregate(OrderRepositoryData orderRepositoryData) {
        this.orderRepositoryData = orderRepositoryData;
    }

    @AggregateIdentifier
    private Integer orderId;

    private OrderRepositoryData orderRepositoryData;

    @CommandHandler
    public void handle(CreateOrderCommand command) {
        apply(new OrderCreatedEvent(command.getOrderId()));
    }

    @EventSourcingHandler
    public void on(OrderCreatedEvent event) {
        this.orderId=event.getOrderId();
        Order order=new Order("Order New");
        orderRepositoryData.save(order);
    }

    protected OrderAggregate() {
        // Required by Axon to build a default Aggregate prior to Event Sourcing
    }

}

There are several things you can remove entirely from this Aggregate, which are:

  1. The OrderRepositoryData
  2. The OrderAggregate constructor which sets the OrderRepositoryData
  3. The manually saving of an Order in the @EventSourcingHandler annotated function

What you’re doing here is mixing the Command Model’s concern of making decisions with creating a queryable Order for the Query Model. It would be better to remove this logic entirely from an Aggregate (the Command Model in your example) and move this to an Event Handling Component.

This is however not the culprit for the AggregateNotFoundException you’re receiving. What you’ve missed is to make the CreateOrderCommand command handler a constructor.

The CreateOrderCommand will create an Order, as it’s name already suggests. Hence, it should be handled by a constructor rather than a regular method. So, instead of this:

    @CommandHandler
    public *void* handle(CreateOrderCommand command) {
        apply(new OrderCreatedEvent(command.getOrderId()));
    }

You should be doing this:

@CommandHandler
public OrderAggregate(CreateOrderCommand command) {
    apply(new OrderCreatedEvent(command.getOrderId()));
}

Hope this helps you out @Sunny!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement