zoukankan      html  css  js  c++  java
  • Nhibernate 3.0 cookbook学习笔记 集合

    Nhibernate支持四种集合:

    1 Bags

    Bags中的数据允许重复,并且顺序在Bag中是不重要的。比如一个ActorRole的Bag集合,可能包含actor role 1, actor role 2, actor role 3, actor role 1,
    actor role 4, and actor role 1,它们中允许有重复项出现。下面是一个典型的Bag映射:

      <bag name="Actors">
        <key column="MovieId"/>
        <one-to-many class="ActorRole"/>
      </bag>

    在相应的类中,Bag的实现可以为IList、ICollection甚至 IEnumerable.

    因为Bag中的数据是可重复的,所以我们不能通过一条简单的SQL语句如:delete from Actors where ActorRoleId='1'来确定删除某一条数据,如果执行这条语句会把三个actor role 1的实体都删除。

    为了解决这个问题,Nhibernate提供了idBag.在idBag中,每一个实体都被分配了一个POID来唯一确定实体。

        <idBag name="Actors">
          <collection-id column="ActorRoleBagId" type="Int64">
            <generator class="hilo" />
          </collection-id>
          <key column="MovieId"/>
          <one-to-many class="ActorRole"/>
        </idBag>

    这样Nhibernate就可以像执行这条语句:delete from Actors where ActorRoleBagId='2'一样来删除唯一的实体。

    2 Lists

     List中的数据也是可重复的,但与Bag不一样,数据的所处位置是有意义的。一个List可能是index 0为actor role 1 , index 1为actor role 2, index 2为actor role 3, index 3为actor role 1,index 4为actor role 4,index 5为actor role 1.一个典型的List映射如下:

    <list name="Actors">
          <key column="MovieId" />
          <list-index column="ActorRoleIndex" />
          <one-to-many class="ActorRole"/>
     </list>

    在相应的类中,List的实现应为IList。
    3 Sets

     Set中的数据是不允许重复的,数据出现的位置也无关紧要。在一个Set中,可能有actor role 1, actor role 3, actor role 2, 和 actor role 4.如果你尝试向Set中插入actor role 1的实体,这个操作将为失败。一个典型的Set映射如下:

        <set name="Actors">
          <key column="MovieId" />
          <one-to-many class="ActorRole"/>
        </set>

    在相应的类中,Set的实现应为来自Iesi.Collections.dll的ISet。
    4 Map

    Map就像一个字典,每一个集合中的实体都拥有一个key和一个value,并且key必须是唯一的。一个典型的映射如下:

        <map name="Actors" >
          <key column="MovieId" />
          <map-key column="Role" type="string" />
          <element column="Actor" type="string"/>
        </map>

    在相应的类中,Map的实现应为IDictionary<string, string>。

    当然,允许映射的数据类型有很多,不单单是string。一个Map的key或value的数据类型甚至可以是一个实体。

        <map name="SomeProperty">
          <key column="Id" />
          <index-many-to-many class="KeyEntity"/>
          <many-to-many class="ValueEntity" />
        </map>
  • 相关阅读:
    Bitcoin core核心客户端在CentOS7上的安装和配置
    Struts的标签及JSTL和EL表达式的使用总结
    Java反编译
    Tomcat 各版本 配置SSI服务实现html模块化实现#include virtual="static/_header.html"
    33
    SQL Server Management Studio无法记住密码
    SQL Server 创建角色和账号
    JS 取出DataGrid 列
    关于HTTP协议的小实验
    DNS服务操作小实验
  • 原文地址:https://www.cnblogs.com/Gyoung/p/2500693.html
Copyright © 2011-2022 走看看