views:

31

answers:

1

Hi Guys,

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

+1  A: 

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>.

Diego Mijelshon

related questions