OUT_OF_RANGE: [AXONIQ-2000] Invalid sequence number 0 for aggregate

Hello, I’m quiet new to axon framework so pardon my basic question
I’m receiving this error [AXONIQ-2000] Invalid sequence number 0 for aggregate while everything is logged showing that the application is going with the flow correctly

@Component
@ProcessingGroup("order-group")
public class OrderEventHandling {
    ....
    ....
    @EventHandler
    public void on(OrderCreatedEvent event) {
        OrderEntity orderEntity = new OrderEntity();
        BeanUtils.copyProperties(event, orderEntity);
        LOGGER.info("+ Event Handled ");
        orderRepository.save(orderEntity);
    }
}



@Aggregate
public class CreateOrderCommandHandler {
    @AggregateIdentifier
    private String userID;
    .......

    @CommandHandler
    public CreateOrderCommandHandler(CreateOrderCommand createOrderCommand) {
        OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent();
        BeanUtils.copyProperties(createOrderCommand, orderCreatedEvent);
        LOGGER.info("+ Command Handler" + orderCreatedEvent.toString());
        AggregateLifecycle.apply(orderCreatedEvent);
    }

    @EventSourcingHandler
    public void on(OrderCreatedEvent event) {
        LOGGER.info("+ OrderCreatedEvent Has been event sourced");
        this.userID = event.getUserID();
        .......
    }

}

The logs

2022-04-03 22:11:36.737  INFO 616831 --- [mandProcessor-0] c.e.a.o.c.e.OrderEventHandling           : + Command HandlerOrderCreatedEvent(userID=27b95829-4f3f-4ddf-8983-151ba010e35b, orderID=037ec3f7-eebb-48ed-9235-b4a1ed528924, productID=ddc2594f-3d90-4269-8547-245d2742b74e, quantity=2, addressID=This is a faw believe it, orderStatus=CREATED)
2022-04-03 22:11:36.739  INFO 616831 --- [mandProcessor-0] c.e.a.o.c.e.OrderEventHandling           : + OrderCreatedEvent Has been event sourced
2022-04-03 22:11:36.758  INFO 616831 --- [mandProcessor-0] c.e.a.o.c.e.OrderEventHandling           : + Event Handled
2022-04-03 22:11:36.812 ERROR 616831 --- [nio-9080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.axonframework.commandhandling.CommandExecutionException: OUT_OF_RANGE: [AXONIQ-2000] Invalid sequence number 0 for aggregate 27b95829-4f3f-4ddf-8983-151ba010e35b, expected 1] with root cause

Okay so after some reading ,trial and error I found out that the aggregate Identifier has to be unique per command to prevent more than one thread from accessing the same aggregate so I changed the identifier to point at the orderID and the error was gone.

My misconception: aggregates is stored and grouped by the identifier :slight_smile:

Well in my case i saw that the error was coming as
if i have a constructor command handler and there is an aggreagate already exists, then axon will not reload it by calling event source handler(i dont know hy axon 4.4), and call the constructor command handler. then constructor command handler when dispatch event and event soruce handler was called, it was throwing this error as out of sequence.

my solution was to add a non constructor command handler(and an empty consturctor without command handler annotation) in the aggregate, and PLUS also have this annotation on the handler.
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)

now axon reloads wherever needed before calling the command handler.
it serves my purpose