0

I have an enity 'Foo' which contains a collection of value objects 'Person', person consist of simple properties like "name" and a a collection of value objects 'NameChange', which is information about previous person names.

I wanted to map it as:

<class name="Foo">
  <bag name="Persons">
    <composite-element class="Person">
      <property name="Name"/>
      <bag name="NameChanges"> <!--WRONG-->
        <composite-element class="NameChange">
          <property name="ExName"/>
        </composite-element>
      </bag>
    </composite-element>
  </bag>
</class>

But NH doesnt allow such mapping.

How to map collection of value objects inside value object?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Alex Burtsev
  • 12,418
  • 8
  • 60
  • 87

1 Answers1

1

Check the doc:

7.2. Collections of dependent objects

Collections of components are supported (eg. an array of type Name). Declare your component collection by replacing the tag with a tag.

<set name="SomeNames" table="some_names" lazy="true">
    <key column="id"/>
    <composite-element class="Eg.Name, Eg"> <!-- class attribute required -->
        <property name="Initial"/>
        <property name="First"/>
        <property name="Last"/>
    </composite-element>
</set>

Note: if you define an ISet of composite elements, it is very important to implement Equals() and GetHashCode() correctly.

Composite elements may contain components but not collections. If your composite element itself contains components, use the <nested-composite-element> tag. This is a pretty exotic case - a collection of components which themselves have components. By this stage you should be asking yourself if a one-to-many association is more appropriate.....

So to answer your question:

How to map collection of value objects inside value object?

Do not do that. Create first level citizens entities, and map them with one-to-many. You gain a lot later, once you will be asked to provide filtering...

Maybe check similar stuff here... My advice/suggestion would be: use mostly one-to-many and many-to-one... I do that 99,7% cases

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I see, _Composite elements may contain components but not collections._ – Alex Burtsev Jan 21 '15 at 13:39
  • Yes exactly. But really, if you plan to have succesful application.. you will soon need searching. I strongly suggest, use standard entities with many to one and one to many. That makes handling and searching easy... trust me ;) Good luck with NHibernate anyhow ;) – Radim Köhler Jan 21 '15 at 13:40