Diego Volpato Diego Volpato - 3 months ago 23
C# Question

NHibernate mapping bag with keys from property of a property

I have the following structure:

public class Version
{
public Status Status;
}

public class Status
{
Action Action;
Area Area;

public IList<Version> Versions
}

public class Action
{
public int Id;
}

public class Area
{
public int Id;
}


And I would like to map the List of versions with a bag, but I might be mapping it the wrong way.
Here is the parte of the Status.hbm.xml file that maps the list:

<bag name="Versions" cascade="save-update" inverse="true" lazy="true" generic="true" order-by="num_version desc">
<key>
<column name="id_action"></column>
<column name="id_area"></column>
</key>
<one-to-many class="Version" />
</bag>


id_action
and
id_area
are foreign keys from
Status
, which is a property of
Version
. Do I need to reference
Status
on this bag mapping?
How am I supposed to map this case?

Thank you

Answer

I was able to find the problem: The mapping I used on the answer is almost correct. However, I needed to inverse the order of the columns on the mapping. because both keys (action and area) must be in the same order as they are defined in the mapping of status.hbm.xml file.

So, the correction was doing just this:

<bag name="Versions" cascade="save-update" inverse="true" lazy="true" generic="true" order-by="num_version desc">
  <key>
    <column name="id_area"></column>
    <column name="id_action"></column>
  </key>
  <one-to-many class="Version" />
</bag>