-->
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.  [ 4 posts ] 
Author Message
 Post subject: Two relationships to the same table?
PostPosted: Mon Apr 03, 2006 5:03 pm 
Newbie

Joined: Fri Jul 15, 2005 4:21 am
Posts: 14
I have a table we can call Parent_Child in a legacy system. This table connects parents and children. The primary key is ParentId and ChildId as a composite-id.

However, both parent and child are really the same object, Person. So I have a many-to-one relationship to Person for both these columns, i.e., two relationships to the same table.

I also have a relationship to another table, so I cannot change the table structure here, but am forced to live with it.

My question is: How do I connect Parent_Child to Person in the hbm.xml files? I can do something like this in ParentChild.hbm.xml:

<many-to-one
name="parent"
class="Person"
column="PARENTID"
insert="false"
update="false"
/>

<many-to-one
name="child"
class="Person"
column="CHILDID"
insert="false"
update="false"
/>

But how do I map this relationship in Person.hbm.xml?

I can do this:

<set
name="parentchildren"
inverse="true"
>
<key
column="PERSONID" />
<one-to-many
class="ParentChild"
/>
</set>

That's one column. How do I map the other one? What do I call the set? Or am I going about this the wrong way? :)

I'd really appreciate any input on this. Thanks.

Kristoffer

Edit: After looking through the manual once again I stumbled onto paragraph 8.4. Could this be what I want? But on the other hand it seems to be based on the fact that Person has a composite primary key, which it has not ...


Last edited by Kristoffer on Tue Apr 04, 2006 8:06 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 7:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
This is a many-to-many with join table. See section 7.5.3. "many to many", of the ref docs. Just swap "Child" in for "Address" :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 7:02 am 
Newbie

Joined: Fri Jul 15, 2005 4:21 am
Posts: 14
Hm, I don't think so ... In 7.5.3 you have three tables, in this case it would be Parent, Child and ParentChild. However, I only have two tables, Person and ParentChild. ParentChild is sort of a join table between two Persons.

Or am I misunderstanding you? :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 10:22 am 
Newbie

Joined: Fri Jul 15, 2005 4:21 am
Posts: 14
I think I solved it.

I now do this in "PersonChild.hbm.xml":

<many-to-one
name="parent"
class="Person"
column="PARENTID"
insert="false"
update="false"
/>

<many-to-one
name="child"
class="Person"
column="CHILDID"
unique="true"
insert="false"
update="false"
/>

Turns out what I really want is a one-to-one relationship between the childId and Person, but a many-to-one relationship between the parentId and Person.

This is how it looks in "Person.hbm.xml" now:


<one-to-one
name="child"
class="PersonChild"
property-ref="child"
/>

<set
name="parents"
inverse="true"
>

<key
column="PARENTID" />
<one-to-many
class="ParentChild"
/>
</set>

Kristoffer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.