단일 테이블 (자식)을 공유하는 여러 테이블 (부모) 간의 관계를 모델링하는 데 어려움을 겪고 있습니다.하나의 자식/다중 부모 테이블 모델링
@Entity
@Table (name="parent1")
public class ParentEntity1 {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
// relationship with child table
@OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<ChildEntity> children;
// other columns of parent 1
}
다음과 같은 자식 테이블 :
는 다음 상위 1 표를 감안할 때 (다른 부모 테이블 2, 3 등은 유사하다)
@Entity
@Table (name="children")
public class ChildEntity {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
// points to the parent type; 1, 2, and so on
@Column(name="parent_type")
private Integer parentType;
@Column(name="parent_id")
private Integer parentId;
// some other data that belongs to ChildEntity
}
은 어떻게 하나를 지속 할 관계를 생성하는 동안 부모 개체 및 여러 자식? 부모 형식은 자식 테이블의 열이며 부모 및 자식 ID는 모두 자동 생성된다는 점에 유의하십시오.
'ChildEntity'의'parentType'과'parentId'는 나쁜 생각입니다. JPA는이를 지원할 수 없습니다. – holmis83
parentType을 없애려면 각 부모에 대해 하나의 하위 테이블을 사용하는 것이 좋습니다.하지만 피할 수는 없지만 대안을 볼 수는 없습니다. – ps0604
데이터베이스 모델이 있습니까? – holmis83