Hi, I have a problem with the usage of @Aggregate members in combination with aggregate polymorphism. I think a simple code example illustrates this best :
The ‘parent’ aggregate
@Aggregate
@NoArgsConstructor
public class School {
@AggregateIdentifier
private String schoolId;
@CommandHandler
public School(CreateSchoolCommand command) {
apply(new SchoolCreatedEvent(command.schoolId()));
}
@EventSourcingHandler
public void handle(SchoolCreatedEvent event) {
this.schoolId = event.schoolId();
}
}
The subclass aggregate with aggregate member:
@Aggregate
@RequiredArgsConstructor
public class PrimarySchool extends School {
@AggregateMember
final Map<String, Group> groups = new HashMap<>();
@CommandHandler
public void handle(CreatePrimarySchoolGroupCommand command) {
apply(new PrimarySchoolGroupCreatedEvent(command.schoolId(), command.groupId()));
}
@EventSourcingHandler
public void handle(PrimarySchoolGroupCreatedEvent event) {
this.groups.putIfAbsent(event.schoolId(), new Group(event.groupId()));
}
}
And the definition of the Group entity :
@AllArgsConstructor
public class Group {
@EntityId
private String schoolId;
}
The issue I have is that this only works if I put the groups aggregate member in the parent (School) aggregate, if it is defined in the PrimarySchool aggregate, then I get the following error :
Exception in thread "main" org.axonframework.commandhandling.CommandExecutionException: Unable to access field for getting.
Caused by: AxonServerRemoteCommandHandlingException{message=An exception was thrown by the remote message handling component: Unable to access field for getting.
Caused by Can not set final java.util.Map field nl.ronob.demo.aggregate.polymorphism.domain.PrimarySchool.groups to nl.ronob.demo.aggregate.polymorphism.domain.School, errorCode='AXONIQ-4002', server='18193@pop-os'}
However, I would like the aggregate member definition in the PrimarySchool aggregate, but I cannot get my head around what I’m doing something wrong ?
(Btw, if needed I can provide a sample spring boot app to replicate this behavior)