NHibernate和集合类型 [英] NHibernate and collection types

查看:59
本文介绍了NHibernate和集合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用Nhibernate 3.0开发我的第一个项目. 我已经阅读了很多材料(博客,论文和样本),并且我认为我可以理解基本知识.
我认为我已经理解了不同类型的馆藏的含义,但是,当我看到互联网上的示例时,我认为我并没有真正理解.
该文档说,当您不希望有重复项时,应使用Set;而当您希望允许重复项时,应使用列表/包. 我发现的大多数示例都是基于您拥有Orders/OrderLines的典型情况. 如果我看一下Order的映射文件,我会看到类似以下的内容:


I am trying to develop my very first project with Nhibernate 3.0. I've gone through loads of material (blogs, papers and samples) and I think I can understand the basics, pretty much.
I think I've understood the meaning of different types of collection but than when I see the examples found on the Internet I think I haven't really understood.
The documentation says that you should use a Set when you do not want duplicates and a List/Bag when you want to allow duplicates. Most of the samples I have found are based on a typical situation where you have Orders/OrderLines. If I have a look the mapping file of the Order I can see something like this:

<class name="OrderHeader"
        table="Orders">
    <id name="OrderId">
        <generator class="hilo"/>
    </id>
    <property name="OrderDate"/>
    <bag name="OrderItems" table="OrderDetails" cascade="all" inverse="true">
        <key column="OrderId"/>
        <one-to-many class="OrderDetail"/>
    </bag>
</class>

<class name="OrderDetail"
        table="OrderDetails">
    <id name="DetailId">
        <generator class="hilo"/>
    </id>
    <many-to-one name="ProductOrdered" column="ProductId"/>
    <many-to-one name="Order" column="OrderId" />
</class>

我曾期望将OrderItems映射为Set;订单将具有唯一的OrderItems?
我对吗? 同时,我希望可以找到Product类一包OrderItems的映射

I had expcted to see the OrderItems mapped as a Set; an order will have unique OrderItems?
Am I right? At the same time I would expect to find a mapping for the class Product a bag of OrderItems

...  

<bag lazy="true" name="OrderItems">
  <key foreign-key="FK_OrderItems_Products">
    <column name="ProductCode" />
  </key>
  .....  
</bag>

...  

在这种情况下,产品将具有非唯一的OrderItems列表.
我有什么想念的吗?
原谅我所有的愚蠢问题:-s

In this situation a product would have a list of non-unique OrderItems.
Is there anything am I missing?
Forgive me all for the silly question :-s

推荐答案

这样做的原因是,正如Yads回答的那样,bag映射与.NET IList<T>映射兼容.通常以这种方式映射集合,以避免在类中引用Iesi.Collections.

The reason for this is that bag mapping is compatible with a .NET IList<T> mapping as Yads answered. Collections are typically mapped that way to avoid having to reference Iesi.Collections in your classes.

对于NHibernate 2.1,您可以通过将集合声明为ICollection<T>并将其初始化为HashSet<T>来使用集合映射.我相信使用NHibernate 3可以将集合声明为ISet<T>.

With NHibernate 2.1, you can use set mapping by declaring the collection as ICollection<T> and initializing it as a HashSet<T>. Using NHibernate 3 I believe you can declare the collection as ISet<T>.

我同意大多数现实世界的收藏都应该映射为集合.

I agree most real world collections should be mapped as sets.

这篇关于NHibernate和集合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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