-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cascade problem with one-to-many that is not a parent-child
PostPosted: Wed Oct 18, 2006 8:44 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
This should be easy, but I am continuing to have problems. I have the following entity objects...

Customer
Address
Contact

A customer HAS zero or more addresses. I have mapped this relationship as a classic parent-child collection.

A customer HAS zero or more contacts. I have also mapped this relationship as a classic parent-child collection.

A contact may or may not WORK AT one of the addresses for the customer. Also, an address may HAVE zero or more contacts that work at the address.

I have a property on the contact called "PrimaryAddress" which holds either a null (if no address has been assigned), or a reference to an address. I have a property on the address which holds the collection of contacts.

I have created a column in the contacts table called PrimaryAddressID which holds the id of the address or a null value.

I have the mapping of the customer --> address and customer --> contacts working fine, and the cascading working as I want it. For example, if a customer id deleted, all of the child addresses and contacts are also deleted from the DB.

HOWEVER, I want to set up a cascade so that when an address is deleted from a customer, AND if any contacts hold a reference to that address as their PrimaryAddress, the PrimaryAddressID column for the contact should be set to null. I cannot figure out a cascade or mapping solution to handle this automatically. A deletion of an address should not delete a contact, but rather set the PrimaryAddressID column to null. This is the problem I am having.

I know this can't be an isolated scenario. Perhaps there is a better way to model the relationships or a way to set up the mapping so cascade works in this way.

Thanks in advance for any ideas!

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 5:22 pm 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
Still running into this same problem, even in other situations. What if I wanted to do something like this...
Code:
Customer cust = new Customer();
cust.Name = "Acme Solutions";
Contact c1 = new Contact();
c1.Name = "John";
cust.AddContact(c1);
Contact c2 = new Contact();
c2.Name = "Sally";
cust.AddContact(c2);

So far, no problems. The customer is the parent, and the contacts are the children. Cascade="all-delete-orphan" will handle deleting the contacts when the parent customer is deleted. This is good.
Code:
Project p1 = new Project();
p1.Name = "Database Upgrade Project";
cust.AddProject(p1);

Again, no problems. Same type of relationship between customer and project, a parent-child with cascade="all-delete-orphan". A project cannot exist without its parent customer.
OK, now, I want to associate a contact with a project.
Code:
p1.PrimaryBillingContact = c1;

No problem. I can even remove the association by setting to null.
Code:
p1.PrimaryBillingContact = null;

But, I do want to assign a billing contact. So...
Code:
p1.PrimaryBillingContact = c1;

Now, here is where my problem comes into play. If I delete contact c1 from the customer's contact collection, I want the PrimaryBillingContact property for Project to be set back to null.
Code:
contactDao.Delete(c1);
Assert.IsNull(p1.PrimaryBillingContact)

But, I cannot figure out a mapping or cascading strategy to handle this effectively. I wanted to simply have PrimaryBillingContact map to a field in my Project table called PrimaryBillingContactID. But, this does not seem to work well when it comes to cascading.

Please... any suggestions?

Thanks.

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 21, 2006 9:21 am 
Beginner
Beginner

Joined: Mon Nov 07, 2005 11:06 pm
Posts: 28
I think I have found a solution. I am going to treat PrimaryBillingContact as a one item collection of Contacts in the Project class. I will map this as a one-to-many collection. I will also add a mapping to the contact class for Projects which will also be a one-to-many. Then, whenever I delete a contact, I will remove all projects prior to this. I will need to store these relationships in a linking table.


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