標籤:style blog class code java c
系列文章
[Nhibernate]體繫結構
[NHibernate]ISessionFactory配置
[NHibernate]持久化類(Persistent Classes)
[NHibernate]O/R Mapping基礎
[NHibernate]集合類(Collections)映射
引言
單向關聯是最常用的也是最難正確使用的。在本文中會逐個經曆規範的案例,從單向映射開始,然後涉及雙向的案例。我們會在所有的例子中hi用Person和Address。例子中沒有包括命名空間和程式集,我們把關注點放在重要的方面。
我們通過是否使用表串連和多樣性(單向或雙向)分類關聯。
在傳統的資料模型中允許為空白的外鍵是不適用的,所以我們的例子中沒有使用允許為空白的外鍵,在NHibernate中這不是必須的,如果你刪除控制的約束,映射會照常工作。
單向關聯
多對一(many to one)
一對一(one to one)
一對多(one to many)
使用表串連的單向關聯
多對一(many to one)
一對一(one to one)
一對多(one to many)
多對多(many to many)
雙向關聯
一對多(one to many)/多對一(many to one)
雙向的一對多(one to many)關聯是普通的關聯類別型。(這是標準的parent/child關係)
1 <class name="Person"> 2 <id name="Id" column="personId"> 3 <generator class="native" /> 4 </id> 5 <many-to-one name="Address" 6 column="addressId" 7 not-null="true" 8 /> 9 </class>10 <class name="Address">11 <id name="Id" column="addressId">12 <generator class="native" />13 </id>14 <set name="People" inverse="true">15 <key column="addressId" />16 <one-to-many class="Person" />17 </set>18 </class>19 create table Person 20 (21 personId bigint not null primary key,22 addressId bigint not null23 )24 create table Address25 (26 addressId bigint not null primary key27 )
一對一(one to one)
使用表串連的雙向關聯
一對多(one to many)/多對一(many to one)
一對一(one to one)
多對多(many to many)
總結
這裡對知識點有個大概的瞭解,具體應用還需在後續的文章中,通過例子來說明。
本文來自《NHibernat 中文文檔》