-->
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: foreign key in one side of one-to-one mapping
PostPosted: Fri Jul 27, 2007 5:33 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Hi all,

I am trying to map a one-to-one relationship, the problem is in the parent table I dont have a reference to the child table, and it is the parent table that I retrive via hibernate.

This dosnt really cause an issue because hibernate maps the two tables ID's.

however I need to use a foreign key in the second table something like:

TableA
ID
name
otherColumn

TableB
ID
tableA_id
name
blah.

so that when I retrive TableA it gets TableB based on the tableA_id column.

I have tried to use both foreign-key and formula attributes in both tables mapping, but I still get the default behaviour.

I would rather avoid having a foreign key in TableA as it might not have a record in TableB and i dont really like having null columns in my data records if i can avoid it.

Any ideas or tips?

Thanks
G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 7:19 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This situation is documented in the hibernate reference:

Code:
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"/>
And this association may be made bidirectional by adding the following to the Person mapping:

<one-to-one name"employee" class="Employee" property-ref="person"/>


So, for your example, in TableA.hbm.xml
Code:
<one-to-one name="BObject" property-ref="AObject"/>

And in TableB.hbm.xml
Code:
<many-to-one name="AObject" column="tableA_id" unique="true"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 8:17 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Ah. I didnt understand when I read that, I wqas a bit worried I would end up with a collection if i used the many to one.

I will give it a try.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 8:56 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
That seems to have solved it perfectly, thank you very much.

G


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.