代码改变世界

NHibernate3剖析:Mapping篇之集合映射基础(1):Set映射

2010-05-08 20:48  李永京  阅读(6705)  评论(10编辑  收藏  举报

本节内容

系列引入

NHibernate3.0剖析系列分别从Configuration篇、Mapping篇、Query篇、Session策略篇、应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种应用程序的集成,基于NHibernte3.0版本。如果你还不熟悉NHibernate,可以快速阅读NHibernate之旅系列文章导航系列入门,如果你已经在用NHibernate了,那么请跟上NHibernate3.0剖析系列吧。

Set映射

我们在设计Domain时,有很多集合形式,我总是设计着各种各样的Domain不断尝试着各种集合形式。集合有所有Net基本类型、NHibernate自定义类型、组件、其他实体的引用等形式,那么在NHibernate中如何通过映射把Domain定义中的集合形式和数据库架构映射起来的呢。Mapping篇为大家介绍基本上所有的Mapping方式,大家学习NHibernate也有个比较"官方"的参考。

这节我们介绍Set映射,一般而言,Domain中的Iesi.Collections.Generic.ISet<T>集合类型使用Set来映射。

映射纲要

Set映射有两个部分:集合外键(Collection foreign keys)和集合元素(Collection element)。

集合外键(Collection foreign keys),通过<key>映射,其column属性命名方式一般采用"集合持有者类名+Id"命名方式。

集合元素(Collection element),即集合中的对象:

  • 值类型,其声明周期完全依赖于集合持有者,通过<element>或<composite-element>映射。
  • 引用类型,被作为集合持有的状态考虑的,只有两个对象之间的“连接”,具有其自己的生命周期,通过<one-to-many>或<many-to-many>映射。

案例分析

案例一:SetOfElements

Domain定义中,集合元素一般是单一元素(Elements)类型,即.Net基本类型(string、int、double等等)。

//Code Snippets Copyright http://lyj.cnblogs.com/
public class SetOfElements
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ISet<string> Elements { get; set; }
}

映射简单写法:

//Code Snippets Copyright http://lyj.cnblogs.com/
  <class name="SetOfElements">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
    <set name="Elements" table="SetOfElementsElements">
      <key column="SetOfElementsId" />
      <element type="String" />
    </set>
  </class>

单一元素类型在NHibernate中使用<element>映射的,我们应该至少指定column和type类型,如果column没指定,NHibernate提供默认列名id。

数据库架构:

SetOfElements

案例二:SetOfComponents

Domain定义中,集合元素一般是组件(Component)类型,也就是值类型。

//Code Snippets Copyright http://lyj.cnblogs.com/
public class SetOfComponents
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ISet<ComponentSet> Components { get; set; }
}
public class ComponentSet
{
    public string StringProperty { get; set; }
    public int IntProperty { get; set; }
}

映射简单写法:

//Code Snippets Copyright http://lyj.cnblogs.com/
  <class name="SetOfComponents">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
    <set name="Components">
      <key column="SetOfComponentsId" />
      <composite-element class="ComponentSet">
        <property name="StringProperty" />
        <property name="IntProperty" />
      </composite-element>
    </set>
  </class>

组件(Component)类型使用composite-element映射,需要指明组件类名,然后映射其组件属性。

数据库架构:

SetOfComponents

案例三:SetOfOneToMany

Domain定义中,集合元素是指向另一个实体OneToManySet的引用。这里设置为一对多关系。

//Code Snippets Copyright http://lyj.cnblogs.com/
public class SetOfOneToMany
{
    public int Id { get; set; }
    public ISet<OneToManySet> OneToMany { get; set; }
}
public class OneToManySet
{
    public int Id { get; set; }
}

映射简单写法:

//Code Snippets Copyright http://lyj.cnblogs.com/
  <class name="SetOfOneToMany">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <set name="OneToMany">
      <key column="SetOfOneToManyId" />
      <one-to-many class="OneToManySet" />
    </set>
  </class>
  <class name="OneToManySet">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
  </class>

我们为SetOfOneToMany和OneToManySet定义一对多关系,使用<one-to-many>映射,指明关联的对象。

数据库架构:

SetOfOneToMany

案例四:SetOfManyToMany

Domain定义中,集合元素是指向另一个实体ManyToManySet的引用。这里设置为多对多关系。

//Code Snippets Copyright http://lyj.cnblogs.com/
public class SetOfManyToMany
{
    public int Id { get; set; }
    public ISet<ManyToManySet> ManyToMany { get; set; }
}
public class ManyToManySet
{
    public int Id { get; set; }
}

映射简单写法:

//Code Snippets Copyright http://lyj.cnblogs.com/
  <class name="SetOfManyToMany">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <set name="ManyToMany" table="SetOfManyToManyToManyToManySet">
      <key column="SetOfManyToManyId" />
      <many-to-many class="ManyToManySet" column="ManyToManySetId" />
    </set>
  </class>
  <class name="ManyToManySet">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
  </class>

两个对象的多对多关系,需要一个“中间表”来存储这两个对象的关系。

数据库架构:

SetOfManyToMany

结语

开始NH剖析的Mapping篇,这篇文章介绍NHibernate中的集合映射之Set映射。

希望本文对你有所帮助。