-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: one-to-many/many-to-one with composite
PostPosted: Mon Jul 21, 2008 3:29 am 
Newbie

Joined: Mon Jul 21, 2008 1:57 am
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 10:43 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a tutorial on doing a one-to-many join:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

And here's a tutorial on compound keys:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=15usingcompoundprimarykeys

Just merge the two concepts together, and you'll have exactly what you need!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.