zoukankan      html  css  js  c++  java
  • NHibernate3剖析:Mapping篇之集合映射基础(3):List映射

    系列引入

    NHibernate3.0剖析系列分别从Configuration篇、Mapping篇、Query篇、Session策略篇、应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种应用程序的集成,基于NHibernte3.0版本号。

    假设你还不熟悉NHibernate。能够高速阅读NHibernate之旅系列文章导航系列入门,假设你已经在用NHibernate了,那么请跟上NHibernate3.0剖析系列吧。

    List映射

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

    映射纲要

    List映射有三个部分:集合外键(Collection foreign keys)、索引字段(index column)、集合元素(Collection element)。

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

    索引字段(index column),用于相应数组或List的索引,是Int类型。有顺序排列的整数(默认base为0,字段名称为idx)。

    通过<list-index>映射。

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

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

    案例分析

    案例一:ListOfElements

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

    //Code Snippets Copyright http://lyj.cnblogs.com/
    public class ListOfElements
    {
        public int Id { get; list; }
        public IList<string> Elements { get; list; }
    }

    映射简单写法:

    //Code Snippets Copyright http://lyj.cnblogs.com/
    <class name="ListOfElements">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
      <list name="Elements" table="ListOfElementsElements">
        <key column="ListOfElementsId" />
        <list-index />
        <element type="String" />
      </list>
    </class>

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

    数据库架构:

    ListOfElements

    案例二:ListOfComponents

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

    //Code Snippets Copyright http://lyj.cnblogs.com/
    public class ListOfComponents
    {
        public int Id { get; list; }
        public IList<ComponentList> Components { get; list; }
    }
    public class ComponentList
    {
        public string StringProperty { get; list; }
        public int IntProperty { get; list; }
    }

    映射简单写法:

    //Code Snippets Copyright http://lyj.cnblogs.com/
    <class name="ListOfComponents">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
      <list name="Components">
        <key column="ListOfComponentsId" />
        <list-index />
        <composite-element class="ComponentList">
          <property name="StringProperty" />
          <property name="IntProperty" />
        </composite-element>
      </list>
    </class>

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

    数据库架构:

    ListOfComponents

    案例三:ListOfOneToMany

    Domain定义中。集合元素是指向还有一个实体OneToManyList的引用。

    这里设置为一对多关系。

    //Code Snippets Copyright http://lyj.cnblogs.com/
    public class ListOfOneToMany
    {
        public int Id { get; list; }
        public IList<OneToManyList> OneToMany { get; list; }
    }
    public class OneToManyList
    {
        public int Id { get; list; }
    }

    映射简单写法:

    //Code Snippets Copyright http://lyj.cnblogs.com/
    <class name="ListOfOneToMany">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
      <list name="OneToMany">
        <key column="ListOfOneToManyId" />
        <list-index />
        <one-to-many class="OneToManyList" />
      </list>
    </class>
    <class name="OneToManyList">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
      <property name="Name" />
    </class>

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

    数据库架构:

    ListOfOneToMany

    案例四:ListOfManyToMany

    Domain定义中,集合元素是指向还有一个实体ManyToManyList的引用。

    这里设置为多对多关系。

    //Code Snippets Copyright http://lyj.cnblogs.com/
    public class ListOfManyToMany
    {
        public int Id { get; list; }
        public IList<ManyToManyList> ManyToMany { get; list; }
    }
    public class ManyToManyList
    {
        public int Id { get; list; }
    }

    映射简单写法:

    //Code Snippets Copyright http://lyj.cnblogs.com/
    <class name="ListOfManyToMany">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
      <list name="ManyToMany" table="ListOfManyToManyToManyToManyList">
        <key column="ListOfManyToManyId" />
        <list-index />
        <many-to-many class="ManyToManyList" column="ManyToManyListId" />
      </list>
    </class>
    <class name="ManyToManyList">
      <id name="Id" type="Int32">
        <generator class="hilo" />
      </id>
    </class>

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

    数据库架构:

    ListOfManyToMany

    结语

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

    希望本文对你有所帮助。

  • 相关阅读:
    Python基础:28正则表达式
    Remove Duplicates from Sorted Array
    Reverse Nodes in k-Group
    Merge k Sorted Lists
    Generate Parentheses
    Container With Most Water
    Regular Expression Matching
    Median of Two Sorted Arrays
    sql 子查询
    linux安装服务器
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7083359.html
Copyright © 2011-2022 走看看