gpt4 book ai didi

java - 将 OneToMany 方映射为所有者

转载 作者:行者123 更新时间:2023-12-01 09:41:36 26 4
gpt4 key购买 nike

我很难理解下面的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将链接到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 表中不存在。

关于java - 将 OneToMany 方映射为所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com