Minwell'Space

最厉害的不是掌握语言的开发人员,而是制定规则的设计人员.

导航

6。关联映射

简介

单向关联是最常用的也是最难正确使用的。在本章中会逐个经历规范的案例,从单向映射开始,然后涉及双向的案例。我们会在所有的例子中使用PersonAddress。例子中没有包括命名空间和程序集,我们把关注点放在重要的方面。

我们通过是否使用表连接和多样性(单向或双向)分类关联。

在传统的数据模型中允许为空的外键是不实用的,所以我们的例子中没有使用允许为空的外键。在NHibernate中这不是必须的,如果你删除空值的约束, 映射会照常工作。

单向关联

 

多对一(many to one)

 

一对一(one to one)

 

一对多(one to many)

 

使用表连接的单向关联

 

一对多(one to many)

 

多对一(many to one)

 

一对一(one to one)

 

多对多(many to many)

 

双向关联

 

一对多(one to many) / 多对一(many to one)

双向的一对多(one-to-many)关联是普通的关联类型。(这是标准的parent/child关系。)

<class name="Person">
<id name="Id" column="personId">
<generator class="native" />
</id>
<many-to-one name="Address"
column="addressId"
not-null="true"
/>
</class>
<class name="Address">
<id name="Id" column="addressId">
<generator class="native" />
</id>
<set name="People" inverse="true">
<key column="addressId" />
<one-to-many class="Person" />
</set>
</class>
create table Person
(
personId bigint not null primary key,
addressId bigint not null
)
create table Address
(
addressId bigint not null primary key
)

一对一(one to one)

 

使用表连接的双向关联

 

一对多(one to many) / 多对一(many to one)

 

一对一(one to one)

 

多对多(many to many)

 

posted on 2006-07-23 15:07  Minwell  阅读(224)  评论(0编辑  收藏  举报