查找收集NHibernate的映射(如果让任何意义) [英] NHibernate mapping of lookup collection (if that makes any sense)

查看:128
本文介绍了查找收集NHibernate的映射(如果让任何意义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么问这样的问题,因为我不知道是什么我也不知道,所以我不知道我想要得到的答案为正确的术语。我会解释我的情况下,在希望它会帮助:

I'm not sure how to ask the question, for I don't know what I don't know, and therefore I don't know the proper terminology for what I'm trying to get the answer to. I will explain my scenario, in hopes that it will help:

我有三个表,一书表,标签表和BookTag查找表

I've got three tables, a Book table, a Tag table and a BookTag lookup table.

每本书都有一个ID,一个标题(对于初学者)
每个标签都有一个ID和一个标题
分别BookTag有一个ID,一个的BookID和一个标签识别

Each book has an ID, a Title (for starters) Each tag has an ID, and a Title Each BookTag has an ID, a BookID, and a TagID.

一个书可以被标记与多个代码,并且一个标签可在一个以上的的BookID使用。

A book can be tagged with multiple tags, and a tag can be used on more than one BookID.

我有这种方式我的对象设置:

I've got my objects setup in this fashion:

Book.cs
int BookID
string Title
List<BookTag> Tags

Tag.cs
int TagID
string Title

BookTag.cs
int ID
int BookID
int TagID

我想Books.cs类有标签的集合,而不是BookTags ,但我似乎无法得到NHibernate的映射权利。这是我已经得到了Book.hbm.xml文件:

I would like the Books.cs class to have a collection of Tags, and not BookTags, but I cannot seem to get the mapping right in NHibernate. This is what I've got for the Book.hbm.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel.Books">
  <class name="DomainModel.Books.Book" table="Books">
    <id name="BookID" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property name="Title" type="String" not-null="true"/>
    <set lazy="true" name="Tags" table="BookTags" generic="true" inverse="true" cascade="delete">
      <key column="BookID"/>
      <one-to-many class="DomainModel.Books.BookTag, DomainModel"/>
    </set>
  </class>
</hibernate-mapping>



这是我BookTag.hbm.xml:

And this is my BookTag.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel.Books">
  <class name="DomainModel.Books.BookTag" table="BookTags">
    <id column="BookTagID" name="BookTagID" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>
    <many-to-one name="Tag">
      <column not-null="true" name="TagID"/>
    </many-to-one>
    <many-to-one name="Book">
      <column not-null="true" name="BookID"/>
    </many-to-one>
  </class>
</hibernate-mapping>



在这种模式下,我可以得到我想要用我的对象模型标记:Book.Tags [0] .TAG,但这似乎只是效率不高。我可以使用NHibernate的绘制出与Tags.TagID的BookTags.TagID在数据库中,这样我可以得到Book.Tags [0]返回一个标签对象,而不是一个BookTags对象吗?我不知道一个更好的方式,使在第一册使用的标签可以在第二册而不添加新条目标签表用于图书的标签联系起来。

Under this model, I can get to the tag I want by using my object model: Book.Tags[0].Tag, but that just seems inefficient. Can I use NHibernate to map out the BookTags.TagID with the Tags.TagID in the database so that I can get Book.Tags[0] to return a Tag object, instead of a BookTags object? I didn't know of a better way to associate Books to tags so that a tag used on Book1 can be used on Book2 without adding a new entry to the Tags table.

我希望这是至少有一些感觉。让我知道如果你需要进一步澄清。如果我弄明白有人回答之前,我会在这里发布我的解决方案。

I hope this makes at least some sense. Let me know if you need further clarification. I'll post my solution here if I figure it out before someone answers.

推荐答案

您不需要BookTag类的所有。您可以映射Book.Tags收集尽可能多的一对多。要做到这一点,你会在地图中指定BookTag连接的关联。看看这里在6.8节双向关联。

You don't need a BookTag class at all. You can map Book.Tags collection as many-to-many. To do this you will specify BookTag in the map to connect the association. Look here in section 6.8 Bidirectional Associations.

这篇关于查找收集NHibernate的映射(如果让任何意义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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