I'm trying to create the mappings for a Person object and a Marriage object where the Person has a
set of marriages and each marriage can refer to the bride and groom, who are Person objects.
Given the following class implementations:
Code:
public class Person {
protected Set<Marriage> myMarriages = null;
public Set<Marriage> getMarriages() {
return myMarriages;
}
private void setMarriages(Set<Marriage> marriages) {
myMarriages = marriages;
}
}
public class Marriage {
protected Person myGroom = null;
protected Person myBride = null;
public Person getBride() {
return myBride;
}
public Person getGroom() {
return myGroom;
}
public void setBride(Person bride) {
myBride = bride;
}
public void setGroom(Person groom) {
myGroom = groom;
}
}
What mapping do I need to implement this model?
For marriage it seems clear that I need something like this:
Code:
<class name="org.egcrc.cfr.common.Marriage" table="MARRIAGE" lazy="true">
...
<many-to-one
name="myGroom"
class="org.egcrc.cfr.common.Person"
column="GROOM_ID"
cascade="none"
not-null="false"
outer-join="false"/>
<many-to-one
name="myBride"
class="org.egcrc.cfr.common.Person"
column="BRIDE_ID"
cascade="none"
not-null="false"
outer-join="false"/>
</class>
But what about Person? It seems that I need a set, so I tried this:
Code:
<class name="org.egcrc.cfr.common.AbstractPerson" table="PERSON" lazy="true">
...
<set name="myMarriages" >
<key column="PERSON_ID"/>
<one-to-many class="org.egcrc.cfr.common.Marriage"/>
</set>
</class>
But this just creates a PERSON_ID column in marriage that refers to either the bride or the groom,
depending on which was set last.
I tried two sets, one using BRIDE_ID and one using GROOM_ID as the key column, but then Hibernate
throws an exception.
Hibernate version: 3.0.2
Name and version of the database you are using:DB2
Code: