-->
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: Problem with optional children in parent/child relationship
PostPosted: Sun Nov 07, 2004 3:44 pm 
Newbie

Joined: Thu Nov 04, 2004 12:52 am
Posts: 4
I'm having trouble implementing an optional association between entities. I have a Customer object with an optional billing Address object. Due to issues with our view technology, the optional child entity - the Address object, in this case - cannot be null.

My first thought was to instantiate an empty Address object in the constructor for the Customer object. The problem with this is that when a Customer entity is retrieved from the database that does NOT have an associated billing address, the Address reference is reset to null.

My second thought was to "lazily" instantiate a new Address object in the getter:

Code:
public Address getBillToAddress(){
    if(billToAddress == null){
        billToAddress = new Address();
    }
    return billToAddress();
}


The problem here is that Hibernate thinks the Customer object has been changed and attempts to persist the new, invalid, Address object, which fails.

I need to basically be able to say somehow that the Address reference - even though it is not null - should be treated as if it were null unless some condition is true (in this case that some of its properties have been set). I've tried implementing Lifecycle in both Customer and Address, and I've tried working with an Interceptor.

I'll be happy to post whatever additional code is necessary. Hope this was clear. Any help with this would be greatly appreciated! Thanks in advance.

Brian


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.