-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to map a unidir one-to-one with fk in the opposite tbl
PostPosted: Tue Jan 27, 2004 4:30 pm 
Newbie

Joined: Wed Jan 21, 2004 5:51 pm
Posts: 14
Location: San Francisco, CA
Is there a way to map a unidirectional one-to-one relationship using a unique foreign key constraint where the foreign key column resides in the table for the target of the relation instead of the source? For example, consider the bidirectional relationship in the documentation, section 5.1.11:

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"/>


How can you model the same relationship if the Employee class has no property pointing back to the person (the many-to-one prop doesn't exist)?

Dave[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Not possible AFAIK. You might be able to use a workaround by using a many-to-one mapping, it might be tricky but possibly work ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think many to one will work either, the FK is on the other table.
Conceptually, this is a one-to-many.

Map it as a one-to-many with private accessor and add an unmapped public getter/setter that do the tricky stuff (update the one-to-many association)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks for suggestions
PostPosted: Tue Jan 27, 2004 9:14 pm 
Newbie

Joined: Wed Jan 21, 2004 5:51 pm
Posts: 14
Location: San Francisco, CA
Thanks for the suggestions. The FK is only in the target table to overcome a deadlock problem with the next leading brand of ORM tool. I'll move the key back to the source table and run some junit tests to make sure Hibernate can hack it.

Thanks, Dave


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 8:07 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 6:58 pm
Posts: 20
Just out of curiosity, is the type of mapping described above (foreign key in target table) an uncommon one, and used primarily in many-to-one mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 8:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is pretty uncommon in the way described. In the relation model, it's done to design many-to-one

_________________
Emmanuel


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