-->
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.  [ 10 posts ] 
Author Message
 Post subject: Silly little one-to-one association question
PostPosted: Thu Jan 15, 2004 4:53 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
How does Hibernate keep track of which objects are involved in a one to one association. I have a one to one association with an Instance object and a CategoryTree object.

The mapping is pretty straightforward:
<one-to-one name="categoryTreeRoot" />

Right now there are about 6 CategoryTree objects persisted. When I get the categoryTreeRoot from the Instance, how does Hibernate know which CategoryTree object to get? I know that it gets the correct one, but I'm not sure how. There don't seem to be any tables or columns that link the two things together. Can someone shed some light on this for me, or at least give me a RTFM and a section number?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The entrys in the two table have the same primary key on a standard one-to-one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:09 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
gloeglm wrote:
The entrys in the two table have the same primary key on a standard one-to-one.

Ah! I see. So if I set the Instance's categoryTreeRoot to another CategoryTree, it won't work, because the primary key won't change. Would a foreign key restraint be more appropriate?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:09 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
I meant constraint, not restraint. It's been a long day...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Of course, Hibernate will update the primary key if you change the association :) This is quite correct, as there may allways be just only one element can be part of the assoication on every side.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:23 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
I'm a bit confused by the one-to-one part of the documentation. It says "if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value! "

Alright... so I don't want to make sure that they are assigned the same identifier value. The primary keys are just IDs here, and I don't want to change them if the association changes.

The next few lines down read:

Quote:
Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>


Why did they use a many-to-one here? Do I need to use a many-to-one to describe my one-to-one?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is just for the a many-to-one looks exactly the same in the java code as a one-to-one. Only a one-to-many will manifest itself as a java collection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:29 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
Maybe I need to explain my situation better. Looking back on my first post I realize that I didn't do such a good job of explaining.

The Instance object has a field of type CategoryTree called categoryTreeRoot. There is only ever 1 Instance object. There will be many CategoryTree objects, but the Instance is only ever associated with 1 of the CategoryTree objects. The rest of the CategoryTree objects are associated with eachother. The CategoryTree is more of a treenode type of object, so the name is a little misleading.

Is one-to-one the best way to go? Do I have to map from both ends of the association?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:30 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
gloeglm wrote:
This is just for the a many-to-one looks exactly the same in the java code as a one-to-one. Only a one-to-many will manifest itself as a java collection.

I just found it odd that they would throw something in about many-to-one in the documentation in the one-to-one section. As a beginner, it's a little confusing to me.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 5:43 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
Okay, I got it figured out now. Thanks for your help, gloeglm.

This is the working mapping:

<many-to-one name="categoryTreeRoot" class="md.search.CategoryTree" column="categoryRoot"/>


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