可能重複:
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