NHIbernate and quotcollection was not an associationquot when using bag and composite-element

Look at this mapping

1
2
3
4
5
6
7
<bag name="searchDefinition" access="field" cascade="all-delete-orphan" table="RawSearchDefinition" fetch="join">
    <key column="ParentObjectId" />
    <composite-element class="RawSearchDefinition">
        <property name="ExcludeKeyword" column="raws_excludeKeyword" type="String" />
        <property name="IncludeKeyword" column="raws_includeKeyword" type="String" />
    </composite-element>
</bag>

It seems ok but it has a subtle error, when you try to save the object,  a “collection was not an association” error will arise. This error is derived from the cascade=”all-delete-orphan”, this attribute is used for real entity object not value ones. A mapping with composite element does not need cascade, the RawSearchDefinition is a value object, its lifecycle will span that of the owner.

The solution is simply to remove cascade attribute, that is not required.

alk.

Tags: NHibernate