If we follow the principles of DDD, one common root should only have links (by id) to another aggregate root (s).
Example:
class Product {
Set<Long> categoryIds;
}
But how can this be achieved with JPA / Hibernate? In jpa, if we want to have, for example, a OneToMany relationship, we defined it as follows:
class Product {
@OneToMany(mappedBy = "", cascade = CascadeType.ALL)
Set<Category> categories;
}
Thus, the JPA approach will contain its own aggregate category roots, which is not recommended in DDD.
How would you design a relationship with JPA, but to comply with DDD principles?
PS: I was thinking of creating a categoriesstring type property and listing a comma-separated category identifier, but is there a better solution?