public class AggregateRoot {
private Integer id;
private Set<Child> children;
}
public class Child {
private Integer id;
private String name;
}
Imagine that you need to save Child
and to send his ID to the some external system. In DDD you will save child with code similar to this:
AggregateRoot aggregateRoot = aggregateRootRepository.getById(id);
Child child = new Child();
child.setName("Sun");
aggregateRoot.addChild(child);
aggregateRootRepository.save(aggregateRoot);
externalService.postSavedChildId(child.getId());
Of course child.getId()
will return null because it is not in persistence context. Any idea how this case should be handled in DDD?