建模单个子/多个父表 [英] Modeling single child/multiple parent tables

查看:150
本文介绍了建模单个子/多个父表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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

@Id
@Column(name =id)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private整数ID;

//与子表的关系

@OneToMany(mappedBy =parentId,fetch = FetchType.LAZY,cascade = CascadeType.ALL,orphanRemoval = true)
私人设置< ChildEntity>儿童;


//父母的其他列1


}

和以下子表:

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

@Id
@Column(name =id)
@GeneratedValue(strategy = GenerationType.IDENTITY)
私人整数ID;

//指向父类型; 1,2,等等
@Column(name =parent_type)
private Integer parentType;

@Column(name =parent_id)
private Integer parentId;

//属于ChildEntity的一些其他数据

}

如何在创建关系时坚持一个父对象和多个子对象?请注意,父类型是子表中的一列,并且父类和子类标识符都是自动生成的。 解决方案

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

  @Entity 
@Table(name =children)
公共类ChildEntity {

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

@ManyToOne
@JoinColumn(name =parent)
私人ParentEntity父;

//一些属于ChildEntity的其他数据
//所有setters和getters / lombok

}

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


@Column(name =parent_type)
private Integer parentType;

//所有setters和getters / lombok
}


@实体
@Table(name =parent1)
public class ParentEntity1 extends ParentEntity {

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

//父级特定的其他列1

//所有setters和getters / lombok
}


@实体
@Table(name =parent2)
public class ParentEntity2 extends ParentEntity {

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

//父级特定的其他列2

//所有setters和getters / lombok
}

并且坚持一个有几个孩子的父母,你可以这样做:

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

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

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

设置< 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继承,您可以将有关每个后续父实体类型的信息存储在其类中。我希望它能解决你的问题。


I'm struggling to model the relationship between multiple tables (parents) that share a single table (child).

Given the following parent 1 table (the other parent tables 2,3 and so on are similar):

@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


}

and the following child table:

@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

}

How do I persist one parent object and multiple children while creating the relationship? Note that the parent type is a column in the child table, and that both parent and child ids are auto-generated.

解决方案

What you should do is to actually attach the ParentEntity to the ChildEntity. In JPA this relation from m:1 is realized by @JoinColumn. You don't need to store parent type in a child entity - it would be a sign of a bad design. Type of a parent should be as close to its entity as possible. So simply put it in that entity:

@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
}

and to persist a parent with several children you do something like that:

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();
// ... 

and that's all. Note that you never instantiate ParentEntity class (as it's abstract). You can have multiple parent tables where each of them refers to single child table by the id. Moreover, you store information about the type of each subsequent parent entity in its class by inheritance from ParentEntity. I hope it solves your problem.

这篇关于建模单个子/多个父表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆