将 OneToMany 端映射为所有者

Map OneToMany side as the owner

我很难理解以下 documentation 评论是什么

We need to duplicate the physical

是什么意思?

使用 insertable=false, updatable=false 有什么意义?
请问,你能帮忙吗?

To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements.

@Entity

public class Troop {

    @OneToMany

    @JoinColumn(name="troop_fk") //we need to duplicate the physical information

    public Set<Soldier> getSoldiers() {

    ...

}


@Entity

public class Soldier {

    @ManyToOne

    @JoinColumn(name="troop_fk", insertable=false, updatable=false)

    public Troop getTroop() {

    ...

}

We need to duplicate the physical

这意味着 Hibernate 将 link 到 Soldier 实体中的列 troop_fk 以连接 Troop 实体。我们必须写连接 TroopSoldier 的列的物理名称。

What's the point of using insertable=false, updatable=false ?

因为它是 Troop 实体的外键,您不能编辑它,因为限制。首先创建 Troop,然后在 Troop.

中添加 Soldier

这个关于 OneToMany 地图的例子,当拥有方是 OneToMany 方时,所以这一方拥有自己的关系,我们假设这一方首先创建。您可以删除 insertable=false, updatable=false 但如果您在数据库中有约束并尝试使用新的 Troop 对象创建 Soldier 您可能会因为这个新的 Troop 实体而出现约束冲突错误Troop table.

中不存在