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: Saving/Deleting Many-To-Many Relationships
PostPosted: Thu Feb 21, 2008 8:16 am 
Newbie

Joined: Thu Feb 21, 2008 7:58 am
Posts: 2
Hi all,

I've been running into some very interesting issues surrounding many-to-many relationships. Basically I am trying to figure out the best, but also the easiest way of saving and deleting many to many relationships using nhibernate.

Let's take the following scenario:

You have a Client.
The Client have Products.
A Product belongs to a Shop.
A Shop has many Products.

Basically we got a many-to-many relationship between Client and Shop with Product being the Association class.

When I create a new Client, I will create the Products too and also associate the Products with a Shop. I will then Save the Client and becuase I have told NHibernate to cascade all-delete-orphan on the Products collection (which has the same setting on the Products collection on the Shop class), the Products will then save automatically.

Now, that works fine, although when I delete the Client, the Products must automatically be deleted, but also from the Shop's Product collection. Unfortunately this isn't working for me as I am getting a "deleted object would be re-saved by cascade" error from NHibernate.

One way to resolve this is to remove all these Products from the Shop's list of Products too. In a simple scenario like this it isn't such a big deal, although when you are sitting with a Client which has lots of these relationships, it is cumbersome to do it for each relationship.

So maybe I have overlooked something and that is why I am asking the NHibernate folk to help me out :)

My question: Is there a way in which to tell NHibernate to automatically remove the associated object (in this case the Product) from all other "Parent" collections?

Thanks to all in advance
Josh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 6:15 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think your problem ist caused by specifiying the cascade on both the client and the shop. Have you tried to change the cascade on the products collection in the shop class ? I wonder if it makes sense to delete the products if they belong to no shop anyway.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 7:31 am 
Newbie

Joined: Thu Feb 21, 2008 7:58 am
Posts: 2
Thanks for the reply,

I've got cascading on both collection properties which is set to "all-delete-orphan". So that when for instance I delete a Client, all the products in its Products collection and on the Shop's Products collection are removed autmatically.

It does not seem that this is possible. And thinking about that, maybe it is a good thing that NHibernate is NOT that intelligent. Which brings me to a situation where the Product needs to be explicitly removed from both collections.

Maybe a bit more work, but at least it shows intent? Writing code to "detach" an object from a domain is probably a good thing, because then your domain is responisble for it's state and will always be correct and not dependant on an ORM to remove the relationship(s)...

hmmm...thinking out load here :)

Josh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 9:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Cascading from client to product seems to be ok for me. When a client is deleted, the products are useless. Only cascading from shop to product makes no sense for me.

I suppose you have to specify cascading on the products collection in client and on the many-to-xxx mapping to shop in product (if you have that one).

What mappings do you have ?

    collection from client to product (maybe with inverse=true)
    many-to-xxx from product to client
    collection from shop to product (maybe with inverse=true)
    many-to-xxx from product to shop


If you only have the collection mappings, I think there is no way for hibernate to cascade from client to product to shop. So when you delete the client the products will be deleted due to cascading and then re-added from shop because they are still in that collection.

I haven't used cascading in such a scenario, yet, so me ideas are base on theory. I hope they are correct ;-) ...

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 9:07 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
sorry, refreshed the wrong page :-(


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.