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.  [ 2 posts ] 
Author Message
 Post subject: Optional relationships
PostPosted: Wed Dec 12, 2007 6:28 pm 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
We have an Address entity (probably actually a value object). Both a Customer and a User can have an Address. Each Address is only associated to a single Customer or User. Typically I have handled this with a unidirectional relationship. You can navigate from Customer or User to Address but not vice versa.

However for a specific business requirement I need to be able to navigate the other way. I can make this work using a one-to-one mapping to Customer and User on the Address (the many-to-one relationships on the Customer and User entities are unchanged).

However when hydrating the the Address from the database a SELECT statement is executed for each possible relationship. I can live with this but eventually Address could be associated to a lot more entities and at that point the superfluous queries would become more of a concern.

Is there an alternative mapping that would better suit my needs?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 12:05 pm 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
No one made any suggestions. What I was looking for was a bag like collection that holds zero or one entities. So in the Address mapping file I used a bag and in the domain object I did this:

Code:
public virtual Customer Customer
{
  get { return _customerList.Count == 0 ? null : _customerList[0]; }
  set
  {
    _customerList.Clear();
    if (value != null)
    {
      value.Address = this;
      _customerList.Add(value);
    }
  }
}


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