Hi
I have spent a few days searching for an answer to my problem. I want to set up one-to-many / many-to-one relationship (with a twist). In real life a Person can be a parent of another person and a child of some other person. So I have Person.hbm and Parentchild.hbm. Parentchild consists of two foreign keys personid1 (parent) and person2(child). person1 and 2 make key for this table. Below are my hbm files. I think my problem is in Personchild.hbm and Personchild.java (setters & getters, hash, equals).
Can someone suggest if hbm is OK?
Hibernate version: 3
Mapping documents:
<hibernate-mapping package="familytree.model">
<class name="Parentchild" table="parentchild">
<composite-id name="parentchild" access="" class="Person">
<key-many-to-one name="parent" column="parent" class="Person"/>
<key-many-to-one name="child" column="child" class="Person"/>
</composite-id>
</class>
</hibernate-mapping>
<hibernate-mapping package="familytree.model">
<class name="Person" table="person">
<id name="id" column="personid">
<generator class="sequence"/>
</id>
<version name="version" column="version"/>
<property name="firstname" column="firststname" not-null="true"/>
<property name="surname" column="surname" not-null="true"/>
<property name="middlename" column="middlename" not-null="false"/>
<property name="gender" column="gender" not-null="true"/>
<property name="dob" column="date" not-null="true"/>
<property name="placeofbirth" column="placeofbirth" not-null="false"/>
<property name="countryofbirth" column="countryofbirth" not-null="false"/>
<property name="dod" column="dod" not-null="false"/>
<property name="placeofdeth" column="placeofdeth" not-null="false"/>
<property name="address1" column="address1" not-null="false"/>
<property name="address2" column="address2" not-null="false"/>
<property name="postcode" column="postcode" not-null="false"/>
<property name="towncity" column="towncity" not-null="false"/>
<property name="country" column="country" not-null="false"/>
<property name="email" column="email" not-null="false"/>
<property name="telno" column="telno" not-null="false"/>
<property name="mobile" column="mobile" not-null="false"/>
<set name="annotations" table="personannotations" lazy="false" cascade="save-update">
<key column="personid"/>
<many-to-many column="annotationid" class="Annotations"/>
</set>
<set name="relationship" table="personrelationship" lazy="false" cascade="save-update">
<key column="personid"/>
<many-to-many column="relationshipid" class="Relationship"/>
</set>
<set name="event" table="personevent" lazy="false" cascade="save-update">
<key column="personid"/>
<many-to-many column="eventid" class="Event"/>
</set>
<set name="tree" table="persontree" lazy="false" cascade="save-update">
<key column="personid"/>
<many-to-many column="treeid" class="Tree"/>
</set>
<set name="partnership" table="personpartnership" lazy="false" cascade="save-update">
<key column="personid"/>
<many-to-many column="partnershipid" class="Partnership"/>
</set>
<set name="parent" lazy="false" cascade="save-update" inverse="true">
<key column="personid" not-null="true"/>
<one-to-many class="Parentchild"/>
</set>
<set name="child" lazy="false" cascade="save-update" inverse="true">
<key column="personid" not-null="true"/>
<one-to-many class="Parentchild"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>familytree/model/Role.hbm.xml</value>
<value>familytree/model/FamilytreeUser.hbm.xml</value>
<value>familytree/model/Person.hbm.xml</value>
<value>familytree/model/Annotations.hbm.xml</value>
<value>familytree/model/Relationship.hbm.xml</value>
<value>familytree/model/Tree.hbm.xml</value>
<value>familytree/model/Event.hbm.xml</value>
<value>familytree/model/Partnership.hbm.xml</value>
<value>familytree/model/Parentchild.hbm.xml</value>
</list>
</property>
</bean>
AIDAN BRANDISH
Already Looked at
http://www.hibernate.org/hib_docs/v3/re ... tance.html