0

我的数据库架构有两个表:-Rule TableRule Scope表。
规则表列(rule_id主键,.....)和规则范围列(rule_id外键,Scope_id不是自动生成的 Id,可以为不同的 rule_id 重复
规则范围主键是rule_id和的组合Scope_id

我的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; 

我有以下问题:-
当我尝试保存 Rule 时,但它没有保留子实体。当坚持违反外键约束的规则范围时,它为规则 ID 提供 0。但是当我使用@JoinColumn 时,它正在工作。请帮助我在坚持和使用的Rule Scope同时坚持下去。RulemappedBy

4

1 回答 1

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()
}
于 2014-12-07T06:50:12.153 回答