天天看點

[NHibernate]關聯映射

系列文章

[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 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 null
23 )
24 create table Address
25 (
26  addressId bigint not null primary key
27 )      

     一對一(one to one)

使用表連接配接的雙向關聯

總結

這裡對知識點有個大概的了解,具體應用還需在後續的文章中,通過例子來說明。

本文來自《NHibernat 中文文檔》

  • 部落格位址:http://www.cnblogs.com/wolf-sun/

    部落格版權:如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起讨論,共同進步!

    再次感謝您耐心的讀完本篇文章。