<分区>
Possible Duplicate:
Hibernate unidirectional one to many association - why is a join table better?
在 Hibernate 在线文档中,在 7.2.3 一对多部分下提到:
unidirectional one-to-many association on a foreign key is an unusual case, and is not recommended. You should instead use a join table for this kind of association.
我想知道为什么?我唯一想到的是,它会在级联删除期间产生问题。例如,Person在外键上引用一对多关系上的地址,地址在person之前会拒绝删除。
谁能解释推荐背后的理由?
这里是引用文档内容的链接:7.2.3. One-to-many
我把实际内容复制粘贴到这里:
A unidirectional one-to-many association on a foreign key is an unusual case, and is not recommended.
<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)
You should instead use a join table for this kind of association.