Hi all,
I'm currently trying to crack Hibernate. Here's what I conceptually want to achieve:
A "Parent" class has a set of "Children". Each "Child" has a Mother (a Parent) and a Father (Parent).
My proposed hbm's are:
Code:
<hibernate-mapping>
<class name="events.Adult" table="ADULT">
<id name="id" column="ADULT_ID">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="events.Child" table="CHILD">
<id name="id" column="CHILD_ID">
<generator class="native"/>
</id>
<many-to-one name="mother" class="events.Adult" column="ADULT_ID"/>
<many-to-one name="father" class="events.Adult" column="ADULT_ID"/>
</class>
</hibernate-mapping>
Now obviously this does not work, as there are two things mapping on to ADULT_ID.
But I'm unsure how to achieve this using Hibernate's mapping model. In legacy database design, I'd have two foreign keys on the child table, mother and father, then to produce a children set for the Parent, i'd do a UNION or simple OR clause in my SQL SELECT.
I'm hopeful I might be able to add something like this to the Adult class:
Code:
<set name="children" inverse="true">
<key column="CHILD_ID"/>
<one-to-many class="Child"/>
</set>
... and magically produce the children set (all who have the Adult as their Father or Mother), but I'm not sure if this is possible with mappings or whether it might needlessly complicate things.
A related problem (and here the biological Parent/Child analogy falls down!), is what happens if I want to have a separate method for getFatheredChildren (Child who has Adult as father) or getMotheredChildren (Child who has Adult as mother). I could add the following to my mapping:
Code:
<set name="motheredChildren" inverse="true">
<key column="CHILD_ID"/>
<one-to-many class="Child"/>
</set>
<set name="fatheredChildren" inverse="true">
<key column="CHILD_ID"/>
<one-to-many class="Child"/>
</set>
Again this doesn't work. If I could persuade it to work, how would Hibernate properly work out which set should contain which items? This doesn't seem to be specified anywhere in the mapping.
Am I approaching this entirely from the wrong direction?
Thoughts appreciated...