2014-12-01 7 views
0

My DB 스키마에는 -및 Rule Scope 테이블의 두 테이블이 있습니다.
규칙 테이블 컬럼 (rule_id 기본 키, .....) 및 규칙 범위 열 (rule_id 외래 키, Scope_id (이 NOT 자동
규칙 범위 기본 키가 rule_id의 조합 다른 RULE_ID
)에 대한 반복 수, 아이디 생성 . 그리고 Scope_idHibernate에서 mappedBy를 사용할 때 하위 엔티티를 유지

RULE 법인

@Entity 
@Table(name = "RULE") 
public class Rule implements IEntity { 

@Column(name = "RULE_ID") 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private int ruleId; 

@OneToMany(fetch=FetchType.LAZY,mappedBy="rule") 
private Set<RuleScope> ruleScope=new HashSet<RuleScope>(); 

Rule Scope 법인 : -

@Entity 
@Table(name = "RULE_SCOPE") 
public class RuleScope { 

@Embeddable 
public static class Id implements Serializable{ 
    @Column(name = "RULE_ID") 
    private int ruleId; 
    @Column(name = "SCOPE_ID") 
    private int scopeId; 
} 

@EmbeddedId 
private Id id = new Id(); 

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
@JoinColumn(name = "RULE_ID", insertable = false, updatable = false) 
private Rule rule; 

다음 질문이 있습니다. -
규칙을 저장하려고하지만 하위 엔터티가 지속되지 않습니다. 외래 키 제약 조건을 위반하는 규칙 범위를 유지할 때 규칙 ID에 0을 제공합니다. 하지만 @JoinColumn을 사용하면 효과적입니다. Rule을 유지하고 mappedBy을 사용하면서 Rule Scope을 유지하도록 도와주세요.

+0

당신이 시도 가지고 : @OneToMany (mappedBy = "rule", fetch = FetchType.LAZY) 개인 집합 ruleScope = new HashSet (); –

+0

동일한 예외가 있습니다. –

+0

현재 OpenJPA를 사용하고있어서 매우 도움이되지 않습니다. ManyToMany, PrimaryKeyJoinColum 또는 JOIN Strategy를 시도해 보겠습니다 ... 나중에 검토하고 다시 변경할 수 있습니다. –

답변

1

이 시도 :

규칙 엔터티 :

@Entity 
@Table(name = "RULE") 
public class Rule implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "RULE_ID", unique = true, nullable = false) 
    private int ruleId; 

    @OneToMany(mappedBy = "id.rule", cascade = CascadeType.ALL) 
    private Set<RuleScope> ruleScope = new HashSet<RuleScope>(0); 

    // Generate Getters, Setters, hashCode() and equals() 
} 

RULE_SCOPE 엔터티 :

@Entity 
@Table(name = "RULE_SCOPE") 
public class RuleScope implements Serializable { 

    @EmbeddedId 
    private Id id; 

    // Generate Getters, Setters 
} 

RULE_SCOPE 복합 PK를 :

@Embeddable 
public class Id implements Serializable { 

    @ManyToOne 
    @JoinColumn(name = "RULE_ID", , nullable = false) 
    private Rule rule; 

    @Column(name = "SCOPE_ID") 
    private int scopeId; 

    // Generate Getters, Setters, hashCode() and equals() 
} 
+0

을 수정하여 업데이트 ... i 수정을 완료하고 작업을 시작했습니다. 나는 다른 접근 방식을 사용했다. –