nHibernate 自连接映射
这可能非常简单,但我现在看不到树木。
为了简洁起见,我想对一个单词对象进行建模,该对象具有与其相关的单词(同义词),这样做时我可以拥有以下映射:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将 Word 实体与单词(同义词)集合进行映射:
您可以在
class
和set
元素以及列中自定义表名称根据需要在key
和many-to-many
元素中命名。请注意,我使用了
set
而不是bag
,因为它更符合语义。您可以将其映射到ICollection
或Iesi.Collections.Generic.ISet
。This would map a Word entity with a collection of Words (Synonyms):
You can customize table names in the
class
andset
elements, and column names in thekey
andmany-to-many
elements as needed.Note that I've used
set
instead ofbag
, as it fits the semantics better. You can map it to anICollection<Word>
orIesi.Collections.Generic.ISet<Word>
.