I have very simple object structure for Person's and their marriage relations. I have exactly same structure in the db as well. (The code is in C# but you can ignore that)
Code:
public class Person
{
public string Id { get; set; }
public ISet<MarriageRelation> MarriageRelations { get; set; }
}
public class MarriageRelation
{
internal int Id { get; set; }
public Person FirstPartner { get; set; }
public Person SecondPartner { get; set; }
public 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).
Code:
<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)?