-->
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.  [ 5 posts ] 
Author Message
 Post subject: how to map a set of one-to-one associations
PostPosted: Thu Aug 10, 2006 7:52 pm 
Newbie

Joined: Fri Jun 16, 2006 12:24 pm
Posts: 7
I have the following Domain objects:

-Person
-Address
- Mailing Address
- Home Address

Mailing address and Home address inherit from Address. A Person can only have one Mailing Address and only one Home Address. Hence, it makes sense for there to be a mapping that retrieves a Set of addresses. It also makes sense for there to be a mapping to get/set a Home Address since there is only one of them.

The addresses are all in the same table, hence I am using table per class hierarchy. The discriminator column is only used to mark what kind of address it is.

What is the best way of dealing with this?

My first approach is to have the somthing like the following:
Code:
public void setMailingAddress(MailingAddress address) {
    Iterator iter = addresses.iterator();
    while(iter.hasNext()){
        Object temp = iter.next();
        if(temp instanceof MailingAddress) then remove the address and replace it with the one passed in and then return from the method
       
    }
    add the address and return
}



Of course the getter would need to do similar logic.


This doesn't seem to be correct to me. Has anyone else delt with this?


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 6:36 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
I'm having a similar problem. Did you find a solution for this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 6:38 pm 
Newbie

Joined: Fri Jun 16, 2006 12:24 pm
Posts: 7
I've gone ahead and used the solution I outlined above. It seems to work, but it still seems very wrong to me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 5:51 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Another way to do this is to have a one-to-one mapping in the hbm file for each of the addresses. Then you could have a getAddresses() method like the following:


Code:
public Set getAddresses() {
    Set temp = new HashSet();
    temp.add(mailingAddress);
    temp.add(homeAddress);
    return temp;
}


I think this is kind of strange too, but it's another way of doing it. I don't know which way would be the "correct" way of doing it.

-Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 6:14 pm 
Newbie

Joined: Fri Jun 16, 2006 12:24 pm
Posts: 7
I agree with you. It does look a little strange. I wonder if we could get some others to comment on these two approaches.


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