-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many set cascade
PostPosted: Tue Apr 06, 2010 5:25 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I have mapped a many-to-many relation like this:
Code:
   <class name="data.Customer" table="customers">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <set name="passes" table="customerPasses" cascade="delete">
         <key column="customer" />
         <many-to-many class="data.Pass" column="pass" />
      </set>
   </class>

   <class name="data.Pass" table="passes">
      <id name="id" column="id">
         <generator class="assigned" />
      </id>

      <set name="customers" table="customerPasses" inverse="true" cascade="delete">
         <key column="pass" />
         <many-to-many class="data.Customer" column="customer" />
      </set>
   </class>

It's between customers and passes.
In the java classes the sets are declared as java.util.Set.

Now I want to clear all the relations related to a pass instance without deleting the instance it self.
I tried Set.clear(), this does clear the set in java but the relations in de DB remain.
I tried to iterate through pass.getCustomers(), which contains the relations from the pass, and delete it like pass.remove(customer instance), same as with Set.clear().
It does work when I iterate the same way but use customer.remove(pass instance).

This isn't very efficient, so does someone know how I can improve this?


Last edited by aukemid on Thu Apr 08, 2010 8:57 am, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Clear many-to-many set
PostPosted: Wed Apr 07, 2010 5:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
It does work when I iterate the same way but use customer.remove(pass instance).


It worked on this side, because it is the 'owner-side' (= not the inverse side) of the relation.
Did you also try if customer.getPasses().clear() might work ?

Anyway Hibernate documentation says, that on bidirectional relations always both sides of associations must be set.
I guess this is also meant for removals, so at least on one side you cannot simply call clear(), but you have to do some iteration.


Top
 Profile  
 
 Post subject: Re: Clear many-to-many set
PostPosted: Wed Apr 07, 2010 5:59 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Thanks. I made the other side the inverse="true" and now it works.


Top
 Profile  
 
 Post subject: Re: Clear many-to-many set
PostPosted: Thu Apr 08, 2010 8:56 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I thought it worked, but the solution created another problem.

When I delete a Pass or a Customer, not only the relation is deleted but the object related to is also deleted. This shouldn't happen.

Can someone help me?

My mappings:
Code:
   <class name="data.Customer" table="customers">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <set name="passes" table="customerPasses" inverse="true" cascade="delete">
         <key column="customer" />
         <many-to-many class="data.Pass" column="pass" />
      </set>
   </class>
Code:
   <class name="data.Pass" table="passes">
      <id name="id" column="id">
         <generator class="assigned" />
      </id>

      <set name="customers" table="customerPasses" cascade="delete">
         <key column="pass" />
         <many-to-many class="data.Customer" column="customer" />
      </set>
   </class>


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