应该持久化双向映射的哪一侧?

huangapple go评论55阅读模式
英文:

Which side of bidirectional mapping should be persisted

问题

I was looking for an answer on SO and several articles, but I think clear explanation is unfortunately missed, so I decided to ask.

Say we have two entities (box contains several items):

@Entity
@Table(name = "box")
class Box
{
    @Id
    private Long id;

    @OneToMany(mappedBy = "box")
    private List<Item> items = new LinkedList<>();

    public void addItem(Item item)
    {
        items.add(item);
        item.setBox(this);
    }
    // setters, getters, delete synchronize method
}
@Entity
@Table(name = "item")
class Item
{
    @Id
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "box_id")
    private Box box;

    // setters getters
}

Which side of the relationship should I persist?

  1. Does it depend on the owning side of the relationship?
  2. Does it depend on the cascade type and owning side has nothing in common?
    1. Place CascadeType.PERSIST in the Box entity, then em.persist(box)
    2. Place CascadeType.PERSIST in the Item entity, then em.persist(item)
    3. Both, and persist is up to me - can be em.persist(box) or em.persist(item)
英文:

I was looking for an answer on SO and several articles, but I think clear explanation is unfortunately missed, so I decided to ask.

Say we have two entities (box contains several items):

@Entity
@Table(name = &quot;box&quot;)
class Box
{
    @Id
    private Long id;

    @OneToMany(mappedBy = &quot;box&quot;)
    private List&lt;Item&gt; items = new LinkedList&lt;&gt;();
    
    public void addItem(Item item)
    {
        items.add(item);
        item.setBox(this);
    }
    // setters, getters, delete synchronize method
}
@Entity
@Table(name = &quot;item&quot;)
class Item
{
    @Id
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = &quot;box_id&quot;)
    private Box box;

    // setters getters
}

Which side of the relationship should I persist?

  1. Does it depend on the owning side of the relationship?
  2. Does it depend on the cascade type and owning side has nothing in common?
    1. Place CascadeType.PERSIST in the Box entity, then em.persist(box)
    2. Place CascadeType.PERSIST in the Item entity, then em.persist(item)
    3. Both, and persist is up to me - can be em.persist(box) or em.persist(item)

答案1

得分: 1

你可以从两个方面持久化关系。

如果你持久化盒子实体,你只需要调用一次实体管理器的持久化方法,并将盒子实体作为参数传递,其中包括项目实体列表,盒子项目参数必须具有级联类型。

如果你持久化项目实体,你将需要调用实体管理器的持久化方法N次,其中N是项目数量,传递项目实体和盒子参数,盒子参数必须具有级联类型。在第一次插入时,Box将没有ID,但一旦插入第一个Item,Box将有ID,因此在接下来的N-1个Item中,你必须使用具有ID的Box,否则它将创建N个Box。

基于所有这些,我认为在大多数逻辑中,更好的做法是持久化父实体,并在表示与子类的关系的属性上使用级联类型的持久化。

英文:

You can persist the relationship from both sides.

If you persist the box entity, you will only have to call the persist method of the entity manager once and pass the box entity as a parameter, with the item entity list, the box items parameter will have to have a cascade type.

If you persist the item entity, you will have to call the persist method of the entity manager N times, being N the number of items, passing the Item entity with the box parameter, the box parameter will have to have a cascade type. In the first insertion Box will not have ID, but once you insert the first Item, Box will have ID so in the following N-1 Items you will have to establish that Box with Id since if not, it will create N boxes.

Based on all this I think that in most logics it is better to persist the parent entity, cob cascade type persist in the property that represents the relation with the daughter class.

huangapple
  • 本文由 发表于 2020年7月29日 05:14:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63142865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定