建模单个子/多个父表

时间:2015-01-19 18:05:04

标签: hibernate jpa one-to-many

我正在努力模拟共享一个表(子)的多个表(父)之间的关系。

给定以下父表1(其他父表2,3等等)相似:

@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


}

和以下子表:

@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和子ID都是自动生成的。

1 个答案:

答案 0 :(得分:0)

您应该做的是将ParentEntity实际附加到ChildEntity。在JPA中,来自m:1的这种关系由@JoinColumn实现。您不需要在子实体中存储父类型 - 这将是一个糟糕设计的标志。父母的类型应尽可能接近其实体。所以简单地把它放在那个实体中:

@Entity
@Table (name="children")
public class ChildEntity {

    @Id Integer parentId;
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne
    @JoinColumn(name="parent")
    private ParentEntity parent;

    // some other data that belongs to ChildEntity
    //all setters and getters / lombok

}

@MappedSuperclass
public abstract class ParentEntity {
    @OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ChildEntity> children;    


    @Column(name="parent_type")
    private Integer parentType;

    //all setters and getters / lombok
}


@Entity
@Table (name="parent1")
public class ParentEntity1 extends ParentEntity {

    @Id 
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;    

    // other columns specific to parent 1

    //all setters and getters / lombok
}


@Entity
@Table (name="parent2")
public class ParentEntity2 extends ParentEntity {

    @Id 
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;    

    // other columns specific to parent 2

    //all setters and getters / lombok
}

并坚持有几个孩子的父母,你做了类似的事情:

ParentEntity pOne = new ParentEntity1();
ParentEntity pTwo = new ParentEntity2();

ChildEntity c1 = new ChildEntity();
ChildEntity c2 = new ChildEntity();
ChildEntity c3 = new ChildEntity();

Set<ChildEntity> childrenOne = new HashSet<>();
childrenOne.add(c1);
childrenOne.add(c2);

Set<ChildEntity> childrenTwo = new HashSet<>();
childrenTwo.add(c3);

pOne.setChildren(childrenOne);
pOne.setType(1);

// ... 
em.getTransaction().begin();
em.persist(pOne);
em.getTransaction().commit();
// ...

pTwo.setChildren(childrenTwo);
pTwo.setType(2);


// ... 
em.getTransaction().begin();
em.persist(pTwo);
em.getTransaction().commit();
// ... 

就是这样。请注意,您永远不会实例化ParentEntity类(因为它是抽象的)。您可以拥有多个父表,其中每个父表都通过id引用单个子表。此外,通过继承ParentEntity,可以在其类中存储有关每个后续父实体的类型的信息。我希望它能解决你的问题。