如何映射集合< T>在NHibernate的? [英] How to map a Collection<T> in NHibernate?

查看:135
本文介绍了如何映射集合< T>在NHibernate的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类联系(基类),一类叫Customer和一类叫做供应商。客户和供应商类从双方联系获得。



客户与订单一个0到n的关系。我想有客户的集合属性,并有它在NHibernate的映射到其对应的表。



这是如何在做NHibernate的(2.0.1版GA)<? / p>

(PS:使用.NET 3.5 SP1,VS2008 SP1)


解决方案

这是这样做的:



创建类是这样的:

 公共类客户:联系
{
私人的ISet<排序> _orders =新HashedSet<排序>();

公文集<排序>订单
{
返回新的List<排序>(_订单);
}

//通常我会返回一个ReadOnlyCollection还< T>而不是一个集合< T>
//因为我想避免用户直接添加订单到集合中。
//如果你们的关系是双向的,那么你必须设置关联另一
//结束还有,为了隐藏这为程序员
//我总是创建添加&安培; remove方法(见下文)

公共无效AddOrder(订购O)
{
如果(O = NULL&放大器;!&安培; _orders.Contains(O)== FALSE)
{
o.Customer =这一点;
_orders.Add(O);
}
}
}

在您的映射,您指定这样的:

 <集名称=订单表=OrdersTable中访问=field.camelcase下划线逆= 真> 
<键列=.../>
<一到多级=订单.. />
< /集>



由于您使用继承,你应该definitly看看不同posibilities关于NHibernate的继承映射,并选择策略是最适合您的情况:
继承映射



关于一套和放大器; bag语义:
- 当你映射一个集合为一组,可以肯定的是,映射的集合中的所有实体,是独一无二的。也就是说,NHibernate的会确保,而重建一个实例,收集将不包含重复。
- 当你收集映射为一个包,有可能加载从数据库对象时您的收藏将包含相同的实体,曾多次




  • 系统设置是作为一个整体考虑不同的
    对象的集合。一组(字母)

    有效的例子是:{A,B,C,D}。每个字母
    发生一次。

  • 一袋一组的推广。一包的
    成员可以有超过
    One会籍而
    组的每个成员只有一个成员。一包的有效
    的例子是{A,A,A,B,C,
    C,D,...}。字母a和c
    出现较袋一次。


I have a class Contact (base class),a class called Customer and a class called Supplier. Customer and supplier class both derive from Contact.

Customer has a 0..n relation with Order. I want to have a Collection property on customer and have it mapped in NHibernate to its corresponding table.

How is this done in NHibernate (version 2.0.1 GA) ?

(ps: using .NET 3.5 SP1, VS2008 SP1)

解决方案

This is done like this:

Create your class like this:

public class Customer  : Contact
{
   private ISet<Order> _orders = new HashedSet<Order>();

   public Collection<Order> Orders
   {
      return new List<Order>(_orders);
   }

   // NOrmally I would return a ReadOnlyCollection<T> instead of a Collection<T>
   // since I want to avoid that users add Orders directly to the collection.
   // If your relationship is bi-directional, then you have to set the other
   // end of the association as well, in order to hide this for the programmer
   // I always create add & remove methods (see below)

   public void AddOrder( Order o )
   {
      if( o != null && _orders.Contains(o) == false )
      {
         o.Customer = this;
         _orders.Add(o);
      }
   }
}

in your mapping, you specify this:

<set name="Orders" table="OrdersTable" access="field.camelcase-underscore" inverse="true">
   <key column="..." />
   <one-to-many class="Order" .. />
</set>

Since you use inheritance, you should definitly have a look at the different posibilities regarding inheritance-mapping in NHibernate, and choose the strategy that is best suited for your situation: inheritance mapping

Regarding set & bag semantics: - when you map a collection as a set, you can be sure that all the entities in the mapped collection, are unique. That is, NHibernate will make sure that, while reconstituting an instance, the collection will not contain duplicates. - when you map the collection as a bag, it is possible that your collection will contain the same entity more then once when loading the object from the DB.

  • A Set is a collection of distinct objects considered as a whole. A valid example of a set (of letters) is: { a, b, c, d }. Each letter occurs exactly once.
  • A Bag is a generalization of a set. A member of a bag can have more than one membership while each member of a set has only one membership. A valid example of a bag is { a, a, a, b, c, c, d, ...}. The letters a and c appear more than once in the Bag.

这篇关于如何映射集合&LT; T&GT;在NHibernate的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆