nHibernate 自连接映射

发布于 2024-08-31 17:02:51 字数 1224 浏览 6 评论 0原文

这可能非常简单,但我现在看不到树木。

为了简洁起见,我想对一个单词对象进行建模,该对象具有与其相关的单词(同义词),这样做时我可以拥有以下映射:

<class name="Word" table="bs_word">
<id name="Id" column="WordId" type="Int32" unsaved-value="-1">
  <generator class="native">
    <param name="sequence"></param>
  </generator>
</id>

<property name="Key" column="word" type="String" length="50" />
<many-to-one name="SynonymGroup" class="BS.Core.Domain.Synonym, BS.Core" column="SynonymId"  lazy="false"/>


<class name="Synonym" table="bs_Synonym">
<id name="Id" column="SynonymId" type="Int32" unsaved-value="-1">
  <generator class="native">
    <param name="sequence"></param>
  </generator>
</id>
<property name="Alias" column="Alias" type="String" length="50" />
<bag name="Words" cascade="none" lazy="false" inverse="true">
  <key column="SynonymId" />
  <one-to-many class="Word"  />
</bag>

像这样映射它意味着对于给定的单词,我可以访问相关单词(同义词) )像这样:

word.SynonymGroup.Words

但是我想知道是否可以将一袋对象映射到单词对象的实例上......如果这有意义,那么我可以像这样访问相关单词:

word.Words

我尝试使用地图元素和复合元素,但都无济于事 - 所以我想知道是否有好心人可以指出我正确的方向?

塔, 克穆01

This is probably incredibly simple, but I just cant see the wood for the trees at the moment.

For brevity, I would like to model a word object, that has related words to it (synonyms), In doing so I could have the following mappings:

<class name="Word" table="bs_word">
<id name="Id" column="WordId" type="Int32" unsaved-value="-1">
  <generator class="native">
    <param name="sequence"></param>
  </generator>
</id>

<property name="Key" column="word" type="String" length="50" />
<many-to-one name="SynonymGroup" class="BS.Core.Domain.Synonym, BS.Core" column="SynonymId"  lazy="false"/>


<class name="Synonym" table="bs_Synonym">
<id name="Id" column="SynonymId" type="Int32" unsaved-value="-1">
  <generator class="native">
    <param name="sequence"></param>
  </generator>
</id>
<property name="Alias" column="Alias" type="String" length="50" />
<bag name="Words" cascade="none" lazy="false" inverse="true">
  <key column="SynonymId" />
  <one-to-many class="Word"  />
</bag>

Mapping it like this would mean for a given word, I can access related words (synonyms) like this:

word.SynonymGroup.Words

However I would like to know if it is possible to map a bag of objects on an instance of a word object...if that makes sense, so I can access the related words like this:

word.Words

I've tried playing around with the map element, and composite elements, all to no avail - so I was wondering if some kind person could point me in the right direction?

ta,
kmoo01

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-09-07 17:02:51

这会将 Word 实体与单词(同义词)集合进行映射:

<class name="Word">
  <id ...>
    <generator .../>
  </id>
  <set name="Synonyms" cascade="all">
    <key />
    <many-to-many class="Word" />
  </set>
</class>

您可以在 classset 元素以及列中自定义表名称根据需要在 keymany-to-many 元素中命名。

请注意,我使用了 set 而不是 bag,因为它更符合语义。您可以将其映射到 ICollectionIesi.Collections.Generic.ISet

This would map a Word entity with a collection of Words (Synonyms):

<class name="Word">
  <id ...>
    <generator .../>
  </id>
  <set name="Synonyms" cascade="all">
    <key />
    <many-to-many class="Word" />
  </set>
</class>

You can customize table names in the class and set elements, and column names in the key and many-to-many elements as needed.

Note that I've used set instead of bag, as it fits the semantics better. You can map it to an ICollection<Word> or Iesi.Collections.Generic.ISet<Word>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文