-->
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.  [ 1 post ] 
Author Message
 Post subject: Correct way to code a one-to-one bidirectional relationship
PostPosted: Mon Feb 12, 2007 8:41 am 
Newbie

Joined: Mon Feb 12, 2007 8:33 am
Posts: 1
What's the correct way to code a one-to-one bidirectional relationship in a POJO?

Something like this perhaps, but you end up with mutual recursion:

User.java
public void setAddress(Address address) {
this.address = address;
address.setUser(this);
}

Address.java
public void setUser(User user) {
this.user = user;
user.setAddress(this);
}

I suppose you could access the member variables directly, but that breaks encapsulation. Or should you avoid this type of relationship, since Users and Addresses both knowing about each other limits re-use?

(Sorry if this is a FAQ; I couldn't find anything by searching or in Hibernate In Action. Should CaveatEmptor include an example of this type of relationship? Bid<>Item is almost there.)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.