可能重复:
Hibernate unidirectional one to many association - why is a join table better?为什么建议避免外键上的单向一对多关联?
在Hibernate的在线文档,根据第7.2.3 1对许多人来说,它提到,认为:
一个单向对外关键字 是一种不寻常的情况, 并且不推荐。您应该使用 而不是使用此关联的此类 的连接表。
我想知道为什么?我唯一想到的是,它可能会在级联删除时产生问题。例如,Person指的是外键上的一对多关系的地址,并且该地址将拒绝在该人面前被删除。
任何人都可以解释推荐背后的理由吗?
这里是链接到参考文档内容:7.2.3. One-to-many
我复制粘贴在这里的实际内容:
单向的一对多关联的外键是一个 异常情况,并不建议。
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses"> <key column="personId" not-null="true"/> <one-to-many class="Address"/> </set> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
create table Person (personId bigint not null primary key) create table Address (addressId bigint not null primary key, personId bigint not null)
你应该使用一个连接表对于这种关联的。基于外键
请参阅http://stackoverflow.com/questions/1307203/hibernate-unidirectional-one-to-many-association-why-is-a-join-table-better – 2012-10-23 11:24:13