@OneToMany 在休眠状态下关联 table
@OneToMany with association table in hibernate
我正在学习休眠课程。讲师讨论了有关数据库的内容。当我们有一个 1-N 关系,并且可选性为 0 时。最好有一个连接 table 来避免空值。只是那不适合我,我希望有人解释一下。明确地说,这个话题基本上是关于冬眠的,我不确定这是否相关。
我认为 table 与 one-to-many 关系的关联是一个糟糕的建议。
首先,你避免空值的论点是错误的——如果你有 parent child 关系,你将 FK 放在 child table 上。因此,如果 children 的数量为零,则根本没有 FK 指向 parent 的记录 - 没有引入空值。
其次,与 child table 上的 FK 相比,提议的映射效率较低。见 The best way to map a @OneToMany relationship with JPA and Hibernate:
For a DBA, this looks more like a many-to-many database association than a one-to-many relationship, and it’s not very efficient either. Instead of two tables, we now have three tables, so we are using more storage than necessary. Instead of only one Foreign Key, we now have two of them. However, since we are most likely going to index these Foreign Keys, we are going to require twice as much memory to cache the index for this association. Not nice!
同样,在14 High-Performance Java Persistence Tips中,单向@OneToMany(Hibernate 映射one-to-many 与关联table 的方式)被列为least-efficient 关系之一。
最后,标题@JoinTable instead @OneToMany
具有误导性——@JoinTable 可用于配置单向@OneToMany 关系,它不会取代它。见 JPA - Using @JoinTable in @OneToMany association:
@JoinTable annotation can be used in an association to customize the generated join table or to map the existing join table. In the following example, we will demonstrates how to use this annotation with @OneToMany association.
我正在学习休眠课程。讲师讨论了有关数据库的内容。当我们有一个 1-N 关系,并且可选性为 0 时。最好有一个连接 table 来避免空值。只是那不适合我,我希望有人解释一下。明确地说,这个话题基本上是关于冬眠的,我不确定这是否相关。
我认为 table 与 one-to-many 关系的关联是一个糟糕的建议。
首先,你避免空值的论点是错误的——如果你有 parent child 关系,你将 FK 放在 child table 上。因此,如果 children 的数量为零,则根本没有 FK 指向 parent 的记录 - 没有引入空值。
其次,与 child table 上的 FK 相比,提议的映射效率较低。见 The best way to map a @OneToMany relationship with JPA and Hibernate:
For a DBA, this looks more like a many-to-many database association than a one-to-many relationship, and it’s not very efficient either. Instead of two tables, we now have three tables, so we are using more storage than necessary. Instead of only one Foreign Key, we now have two of them. However, since we are most likely going to index these Foreign Keys, we are going to require twice as much memory to cache the index for this association. Not nice!
同样,在14 High-Performance Java Persistence Tips中,单向@OneToMany(Hibernate 映射one-to-many 与关联table 的方式)被列为least-efficient 关系之一。
最后,标题@JoinTable instead @OneToMany
具有误导性——@JoinTable 可用于配置单向@OneToMany 关系,它不会取代它。见 JPA - Using @JoinTable in @OneToMany association:
@JoinTable annotation can be used in an association to customize the generated join table or to map the existing join table. In the following example, we will demonstrates how to use this annotation with @OneToMany association.