How to change state of an aggregate in update event scenario.

Hi,

I am new to axon framework and playing around few scenarios.I am facing an issue with one of the scenario like below.

I have an HiringRequest object and it is having list of HiringRequestNeeds so i have created HiringRequest aggregate with multiple HiringRequestNeeds.

For example: I have one HiringRequest and it has two HiringRequestNeeds,if i want to update one of HiringRequestNeed,how do i change the state of an aggregate with exact updated HiringRequestNeed because HiringRequestNeed doesn’t have any identifier to identify which HiringRequestNeed to be updated.

//NOTE : setting HiringRequestAggregate’s id as target aggregate id of CreateHiringRequestNeedCommand.

public class HiringRequestAggregate {

/** Reference to the aggregate identifier which is typically a string representation of UUID */

@AggregateIdentifier
private String id;

private List needs = new ArrayList();

@CommandHandler
public HiringRequestAggregate(final CreateHiringRequestCommand command) {
apply(HiringRequestCommandsMapper.INSTANCE.eventForCreateHiringCommand(command));
}

@EventSourcingHandler
public void on(final HiringRequestCreatedEvent event) {
this.id = event.getTargetAggregateId();

@CommandHandler
public void createHiringRequestNeed(CreateHiringRequestNeedCommand command){
HiringRequestNeedCreatedEvent hiringRequestNeedCreatedEvent = HiringRequestCommandsMapper.INSTANCE.eventForCreateHiringRequestNeedCommand(command);
apply(hiringRequestNeedCreatedEvent);
}

@EventSourcingHandler
public void on(HiringRequestNeedCreatedEvent hiringRequestNeedCreatedEvent){

HiringRequestNeed hiringRequestNeed = new HiringRequestNeed();
hiringRequestNeed.setComments(hiringRequestNeedCreatedEvent.getComments());
this.needs.add(hiringRequestNeed);
}

@CommandHandler
public void updateHiringRequestNeed(UpdateHiringRequestNeedCommand updateHiringRequestNeedCommand){

HiringRequestNeedUpdatedEvent hiringRequestNeedUpdatedEvent = HiringRequestCommandsMapper.INSTANCE.eventForUpdateHiringRequestNeedCommand(updateHiringRequestNeedCommand);
apply(hiringRequestNeedUpdatedEvent);
}

@EventSourcingHandler
public void on(HiringRequestNeedUpdatedEvent hiringRequestNeedUpdatedEvent){

// how to change state of an aggreate with updated HiringRequestNeed
}

}

Hi,

If I follow your question correctly your interested in how specifically adjust an HiringRequestNeed out of the list in your HiringRequest aggregate.
I’d suggest changing the HiringRequestNeed into an entity.
With the example code you’ve giving, you’d have to take the following steps:

  • Add a HiringRequestNeed identifier field, which you annotate with the @EntityId annotation

  • Add the @AggregateMember annotation to your List, so that Axon knows you want to route commands to the HiringRequestNeed entity

  • Add the HiringRequestNeed identifier field to the commands which should eventually change the state of a single HiringRequestNeed through their corresponding events.
    I hope this helps, and feel free to ask follow up questions!

Cheers,

Steven

Hi Steven

Thank you so much for your reply. We will follow your suggestions and post back with results.

Thanks,
Chandra.