views:

308

answers:

0

I have very simple object structure for Person's and their marriage relations. I have exactly same structure in the db as well.

public class Person
{
    public virtual string Id { get; set; }
    public virtual ISet MarriageRelations { get; set; }
}
public class MarriageRelation
{
    internal virtual int Id { get; set; }
    public virtual Person FirstPartner { get; set; }
    public virtual Person SecondPartner { get; set; }
    public virtual DateTime? MarriageDate { get; set; }
}

I'm having trouble for defining mapping for the MarriageRelation class. I couldn't use many-to-many because of additional fields of the relation (in reality there are more fields, this is simplified view).


<class name="Person, MyLibrary">
  <id name="Id" column="ID" type="String">
    <generator class="assigned" />
  </id>
  <set name="MarriageRelations" cascade="all">
    <key column="FirstPartner" />
    <one-to-many class="MarriageRelation, MyLibrary" />
  </set>
</class>
<class name="MarriageRelation, MyLibrary">
  <id name="Id" column="ID" type="Int32" >
    <generator class="native" />
  </id>
  <property name="MarriageDate" />
  <many-to-one class="Person, MyLibrary" name="FirstPartner" />
  <many-to-one class="Person, MyLibrary" name="SecondPartner" />
</class>

There are at least two problems: - How can I map Person.MarriageRelations so that if Person can be FirstPartner or SecondPartner in the relation. - Everytime I add a new marriage, nhibernate makes an insert (correctly) and then an update which sets the secondpartner same with firstpartner.

Any suggestions to make this work (or model this relation in a different way)?