2
JPA (또는 최대 절전 모드)에서 여러 레벨을 유지할 수 있습니까? 각 리소스가 다른 리소스의 "하위"가 될 수있는 테이블 "리소스"가 있습니다. 이것은 여러 단계를 통해 발생할 수 있습니다. 관계를 포함하기 위해 조인 테이블을 사용하고 있습니다.JPA, 최대 절전 모드 : 여러 레벨 유지하기
내가 달성하고 싶은 것은 이것입니다.
resource resource_relations
======== ==================
resource_type | resource_name parent | child
------------------------------ --------------------
type1 | P P | C
type2 | C C | G
type3 | G
내 영속성 엔티티는 다음과 같습니다.
자원
private String name;
private ResourceType resourceType;
public Resource(resourceType, name){ ... }
@OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST)
private Set<ResourceRelation> components = new HashSet<ResourceRelation>();
@OneToMany(mappedBy = "child", cascade = CascadeType.PERSIST)
private Set<ResourceRelation> parents = new HashSet<ResourceRelation>();
public void addComponent(Resource r) { /*add "r" to "components"*/ }
@ManyToOne (cascade = CascadeType.PERSIST)
private Resource parent;
@ManyToOne (cascade = CascadeType.PERSIST)
private Resource child;
지금, 나는 다음과 같은 문장을 실행 ResourceRelation :
parent = new Resource(type1, P);
child = new Resource(type2, C);
grandChild = new Resource(type3, G);
child.addComponent(grandChild);
parent.addComponent(child);
persist(parent);
는 그러나, P와 C가 점점 지속하지만G. 어떻게해야합니까?
"리소스"와 "리소스 간의 관계"는 두 가지 별도의 항목입니다. 즉, "자원 간 관계"는 자원을 만든 후에 언제든지 지정할 수 있습니다. 그래서, 나는 관계형 테이블을 위해 별도의 엔터티가 필요할 것이라고 생각한다. – Neo
아니요 리소스 사이의 관계는 리소스 생성 후에도 구성 요소 또는 부모 목록에서 요소를 추가/제거 할 때마다 Hibernate에 의해 자동으로 작성/업데이트됩니다. 계단식 유형을 모두 ALL으로 수정해야합니다. – overmeulen