-->
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: Unidirectional OneToOne with foreign key on opposite side
PostPosted: Wed May 21, 2008 12:04 pm 
Newbie

Joined: Wed May 21, 2008 11:52 am
Posts: 5
Hi,

is there a way to accomplish this mapping:

* 2 classes: Person and Address
* Person references Address (to-one), but not the other way round
* 2 tables: PERSON and ADDRESS
* atypical foreign key PERSON_ID in table ADDRESS references PERSON.ID

Is this mapping possible without having to declare a property "person" in class "Address"?

Kind regards,
Frank


Top
 Profile  
 
 Post subject: Not possible.
PostPosted: Wed May 21, 2008 1:44 pm 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Based on my current experience and reading the example of Bid and Item in Java Persistance with Hibernate book.. this does not seem to be possible.

However, I am trying to abstract out this dependency in a way that id related to forignkey etc are inherited and could be used by more than one class with out have to repeat the code in every fp class..

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject: Re: Not possible.
PostPosted: Wed May 21, 2008 2:43 pm 
Newbie

Joined: Wed May 21, 2008 11:52 am
Posts: 5
nagkumar wrote:
Based on my current experience and reading the example of Bid and Item in Java Persistance with Hibernate book.. this does not seem to be possible.


Thank you for confirming my suspicion.

In the depicted constellation the to-one relationship Person->Address is essentially not an many-to-one but a "degenerated" one-to-many relationship. I hoped there would have been some (undocumented) mapping.

Kind regards,
Frank

Btw: OpenJPA seems to supports this constellation, also undocumented but pure JPA:

Code:
@Entity
public class Person {
   @OneToOne
   @JoinColumn(name="Address.Person_id", referencedColumnName="id")
   private Address address;
...
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 4:10 am 
Newbie

Joined: Wed Nov 16, 2005 6:09 pm
Posts: 5
Would a @ManyToOne not achieve your goal? Especially given this is not a 'true' one-to-one relationship.

Code:
@Entity
public class Person {
   @ManyToOne
   @JoinColumn(name="Address.Person_id", referencedColumnName="id")
   private Address address;
...
}


Ultimately you get a reference to Address in Person without having to declare any relationship in Address. On the other hand, if this is truely a @OneToOne then why is having both ends of the relationship a problem? Or have I totally missed the point?


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.