DDD 원칙을 따르면 하나의 집계 루트는 다른 집계 루트에 대한 참조 (ID 별) 만 가져야합니다.DDD : JPA/Hibernate 엔티티 관계로 올바르게 구현하는 방법?
예 :
// Product Aggregate root
class Product {
// References to categories Aggregate Roots (to ids)
Set<Long> categoryIds;
}
그러나이 JPA 달성 할 수있는 방법/최대 절전 모드? 우리가하려는 경우 는 JPA에서, 예를 들어, OneToMany 관계, 우리는 다음과 같이 정의 :
// Product Aggregate root
class Product {
// Holds category aggregate roots
@OneToMany(mappedBy = "", cascade = CascadeType.ALL)
Set<Category> categories;
}
그래서 JPA-의 접근 방식 DDD 권장되지 않는, 카테고리 집계 뿌리 자체를 개최한다.
어떻게 JPA와의 관계를 설계하겠습니까? 그러나 DDD 원칙에 맞습니까?
추신 : 나는 categories
문자열 유형의 속성을 생각하고 쉼표로 구분 된 범주 ID 목록을 보유하고 있지만 더 좋은 해결책이 있습니까?
@Entity
public class Product {
@Id
@GeneratedValue
private int id;
@OneToMany
@JoinTable
private Set<Category> categories;
// constructor, getters, setters, etc...
}
@Entity
public class Category {
@Id
@GeneratedValue
private int id;
// constructor, getters, setters, etc...
}
그냥 예를 들어 내가 몇 함께 연결합니다 :
'캐스케이드 = CascadeType.ALL'가 잘못된 방향으로 향하고있다 그 두 집계를 동일한 트랜잭션에 연결합니다. –