I am new to DDD and I am stuck in here: I have a many-to-many relationship between two entities: User and Group. THe relationship is not an Aggregate because a User can exists without a Group and a Group can exists without a User.
THis is my code for User class:
public class User {
private List<Group> groups = new ArrayList<Group>();
private UserRepository userRepository;
public void create() throws Exception{
userRepository.create(this);
// I have to update the groups with the user.
for (Group group : groups) {
group.update();
}
}
public void addGroup(Group group){
if (!groups.contains(group)) {
groups.add(group);
group.addUser(this);
}
}
}
The problem is that I dont know where to associate those classes when I create a User which has groups in (I cannot use ORM). I made it in the create method of User, and also manage transactions there through Spring. Is this correct? Or should I put that code in the userRepository or in a Service?
Thanks!