Search code examples
spring-bootmicroservicescqrsevent-sourcingaxon

org.axonframework.eventsourcing.IncompatibleAggregateException (Axon framework: Aggregate identifier must be non-null after applying an event)


I try to config cqrs and event sourcing with axon. SeatReseveCreateCommand is work properly. but SeatReserveUpadateCommand is not work correct.

this is my SeatReserve aggregate

@Aggregate
public class SeatReserve {
    @AggregateIdentifier
    private String id;
    private String seatid;
    private Date date;

    @SuppressWarnings("unused")
    private SeatReserve() {
    }

    @CommandHandler
    public SeatReserve(SeatReseveCreateCommand seatReseveCreateCommand) {
        apply(new SeatReseveCreateEvent(seatReseveCreateCommand.getMyid(), seatReseveCreateCommand.getSeatId(),
                seatReseveCreateCommand.getDate()));
    }

    @CommandHandler
    public SeatReserve(SeatReserveUpadateCommand upadateCommand) {
        apply(new SeatReserveUpadateEvent(id, upadateCommand.getSeatId()));
    }

    @EventSourcingHandler
    public void on(SeatReseveCreateEvent seatReseveCreateEvent) {
        this.id = seatReseveCreateEvent.getId();
        this.seatid = seatReseveCreateEvent.getSeatId();
        this.date = seatReseveCreateEvent.getDate();
    }

    @EventSourcingHandler
    public void on(SeatReserveChangeEvent upadateEvent) {
        seatid = upadateEvent.getSeatId();
    }

}

this is my controller

@RestController
public class TestController {

    private final CommandGateway commandGateway;

    public TestController(CommandGateway commandGateway) {
        this.commandGateway=commandGateway;
    }

    @PostMapping
    public String fileComplaint(@RequestBody Map<String, String> request) {
        String id = UUID.randomUUID().toString();
        SeatReseveCreateCommand command=new SeatReseveCreateCommand(id,request.get("seatid"),new Date(request.get("date")));
        commandGateway.send(command);
        return id;   
    }
    @PatchMapping
    public String fileComplaintUpdate(@RequestBody Map<String, String> request) {
        SeatReserveUpadateCommand command= new SeatReserveUpadateCommand(request.get("id"),request.get("seatid"));
        commandGateway.send(command);
        return request.get("id");
    }
}

I try to send request using postman

this is my create request

enter image description here

this is my update request

enter image description here

update make this error

2018-01-03 10:44:53.608  WARN 11138 --- [nio-8085-exec-1] o.a.c.gateway.DefaultCommandGateway      : Command 'com.thamira.research.api.bankaccount.SeatReserveUpadateCommand' resulted in org.axonframework.eventsourcing.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.)

how can I solve this.


Solution

  • The problem is that your update command is defined as a constructor. The command should go to the already existing aggregate instance.

    Changing the command handler to:

    @CommandHandler
    public void handle(SeatReserveUpadateCommand upadateCommand) {...}
    

    should fix the issue.