-->
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.  [ 5 posts ] 
Author Message
 Post subject: Example of compound key parent/child relatonship?
PostPosted: Tue Dec 23, 2003 11:51 am 
Newbie

Joined: Thu Dec 11, 2003 1:12 pm
Posts: 4
I'm just starting out, testing Hibernate, and attempting to model a parent/child relationship onto a legacy database.

My schema looks like this:
Code:
table parent
  parentid
  <data fields>

table child
  parentid
  childid
  <data fields>

In the child table, the key is (parentid, childid). Note that childid is not unique across the table; it numbers 1...n for each instance of parent.

I'm not sure how to model this properly for a one-to-many inverse relationship. I've gotten a simple parent->child set to work, but not a relationship where the child can access its parent.

Any samples out there? I understand that surrogate keys would be best, but adding them is not an option right now.

Thanks,
Steve


Top
 Profile  
 
 Post subject: Re: Example of compound key parent/child relatonship?
PostPosted: Tue Dec 23, 2003 1:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
sdorato wrote:
I've gotten a simple parent->child set to work, but not a relationship where the child can access its parent.

You should since your child refers parent_id, and thus parent.

The one-to-many is mapped as a plain old one-to-many, no particularity
Code:
<set ...
  <one-to-many class="Child" column="parentid">
</set>

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 1:23 pm 
Newbie

Joined: Thu Dec 11, 2003 1:12 pm
Posts: 4
Thanks. But the parent -> child relationship was not the problem. It was the reciprocal relationship--the child accessing the parent.

The composite-id of (parentid, childid) didn't seem to get along with the <many-to-one> mapping from the child.

I got around this by modifying the child hbm.xml mapping so it doesn't consider the parent id part of its key. (No composite id block, just one <id> of childid.) The parent id is only mentioned in a <many-to-one> element. However, now the parentid field in the child is not initialized at all.

Perhaps I should forgo having both a parent reference and a parentid field in my child class...? If I want the parent's id I can always call child.getParent().getId()....

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use <key-many-to-one> Have a look at the dtd

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue Dec 23, 2003 1:52 pm 
Newbie

Joined: Thu Dec 11, 2003 1:12 pm
Posts: 4
Now that you said it, I see it there in the reference, under <composite-id>. Missed it before. Thanks.

Steve


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