[NHibernate]關聯映射

來源:互聯網
上載者:User

標籤: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 中文文檔》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.