-->
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: One-To-One Cascasde Question
PostPosted: Tue Oct 17, 2006 8:39 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
I have the following scenario and I cannot figure out how to set up the cascade.

I have a Customer object that "owns" a collection of Address objects. This relationship is handled from the Address side of things.

I also have a Contact object which I want to associate with an Address, but the Contact is not the parent of the address. I have a PrimaryAddress property on the Contact that holds a reference to the Address object. I am using the foreign-key approach to mapping this one-to-one relationship, which requires a PrimaryAddressID column in the Contacts table. So far, so good.

If I set Contact.PrimaryAddress = null, it works as expected and the PrimaryAddressID column is set to null. Assigning a valid Address object also works as expected.

However, I would like the PrimaryAddressID column to be set to null automatically if an Address is deleted from the parent Customer. Right, now, when I delete an address, the PrimaryAddress column in the Contacts table still has the ID of the deleted Address.

I did not want to put the cascade rule on the Contact mapping because the contact does not "own" the address. Also, I am not sure what cascade value I would use on the Address side of things to remove ID value from the Contacts table.

I am stuck on this one. Any help greatly appreciated!

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 10:21 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Kevin,

I do not have a solution here, but it is an interesting problem so I will ask a different question: if you want the Address deletion to change the Contacts table don't you need to create a link between the two in the java model? Is this something your model allows?

In this case the Address may be able to manage the relationship to Contacts.

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 1:11 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
I am working in .Net, but that should not matter. In my model, an Address must have a parent Customer, but may or may not be related to a Contact. The primary relationship is between Customer and Addresses. But, I want to be able to relate other objects in my model to an address. So, in the example I described, I may want a Contact to be associated to an Address, or some other object to be associated with an Address. These other objects may hold a reference to an Address, but if I delete a Contact for example, the Address would not be deleted. An address can only be deleted directly, or if the parent Customer is deleted.

So...
- An address is always associated with the parent Customer through a Customer property.
- An address may be associated with zero or more "other" objects, such as a Contact or another TBD object.

I am not worried about navigating from an Address to one of these other objects, just the other way around.

So, my problem is how to automatically manage the references to an Address in these other objects when the Address is deleted.

OR...

Maybe I should manage this relationship from the Address side of things. For example, an Address may have zero or more contacts associated with it. Maybe I need to simply map a one-to-many association from Address to Contact and a many-to-one association from Contact to Address. This turns around my original thinking, but it seems right.

Any more thoughts?

Kevin

[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 2:53 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Oops, sorry for the language mismatch :)

I think it may prove useful in itself to link addresses to contacts, from an end user standpoint.

What will you do with the contacts that are not associated with an address? How will you get to them, will they just stay in the database and gather dust?

Another way to approach the problem is from the database side. Just create some triggers and be done with it :)

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 3:24 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
I forgot to mention that Contacts are also a child collection from Customer. I think I will go ahead and attempt to create the Address-Contacts relationship and see how it goes.

Thanks for your input!

Kevin


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.