-->
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: bidirectional one-to-one association problem
PostPosted: Fri Nov 14, 2003 10:43 pm 
Newbie

Joined: Fri Nov 14, 2003 9:25 pm
Posts: 4
Hi,

Could really use some mapping help.

I have an object graph where one class is associated to another via a 0/1. I implemented this with a one-to-one association. I defined the association at both ends initially.

My problem is that when I create a new child class at the 0/1 end for an existing parent and persist the child, I get a new parent row instead of the child being tied to the old parent row.

After much hair-pulling, I discovered that when I made this association uni-directional from child to parent, it saved properly. When the association was defined at both ends, I got the extra row. Looking back at the hibernate examples in the reference guide, their only example of a one-one is unidirectional (person -> author). I am not sure why having the parent know about the child caused a new parent row. Some kind of circular reference bug? Me being confused (entirely likely)?

Thanks,

Jim


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 6:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Basicly, the child must have the same id as the parent.
Either use the foreign id generator or manually assigned it.

See the following references to the doc.

One-to-one associations don't need an extra table column; if two rows are related by a one-to-one association then the two table rows share the same primary key value. So if you want two objects to be related by a one-to-one association, you must make sure that they are assigned the same identifier value!

foreign (id generator)
uses the identifier of another associated object. Used in conjunction with a <one-to-one> association.

And the FAQ http://www.hibernate.org/118.html#A9

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hibernate 2.1 allows a one-to-one using a foreign key. One side is mapped as a one-to-one and reference to the property representing the foreign key, eg, the object. The other side is mapped as a many-to-one. Read to docs with the 2.1 release.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 2:26 am 
Newbie

Joined: Fri Nov 14, 2003 9:25 pm
Posts: 4
Hi,

Yes, I figured out the fact that the primary keys had to be the same before posting this problem, although that did take me some time. From what I could tell, both the pre-existing parent and the new child had the same primary key at the point where I tried to persist the child, yet I still got the new parent row. It wasn't until I broke one direction on the association that it started to behave properly. I will verify this tomorrow - was working on it too late and getting frustrated, so hopefully I can figure out what I was doing wrong upon a fresh start.

Jim


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.