-->
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: Collection members not being deleted
PostPosted: Wed Mar 29, 2006 7:04 am 
Newbie

Joined: Wed Mar 29, 2006 6:52 am
Posts: 4
Hello,

I've just started using Hibernate and have come across a small problem. I have a class RouteSet, which contains a Set of class Route (defined as a one-to-many relationship).

When my app starts, I retrieve my RouteSet object from the database. My problem is this: If I then reassign the underlying set reference using RouteSet.setRoute(Set<Route> newRoutes) and persist the object, any Route objects no longer in the Set are not deleted from the database; instead their foreign key is set to null.

I thought that setting cascade="all-delete-orphan" on RouteSet would fix this problem, but it doesn't appear to. Does anyone have any ideas?

Regards,
Adam.

Extract from RouteSet.hbm.xml:

Code:
<set name="routes" cascade="all-delete-orphan" lazy="false">
  <key column="routeSetId" />           
  <one-to-many class="Route" />
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 7:37 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Use this:

RouteSet.getRoute().clear();
RouteSet.getRoute().addAll(your new set);

If it doesn't work, post more details (mappings for RouteSet and Route).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:11 am 
Newbie

Joined: Wed Mar 29, 2006 6:52 am
Posts: 4
Hi Tom,

I'm afraid doing a clear() followed by addAll() failed to solve the problem. However, I realise I'd fallen into a common newbie trap to do with parent/child relationships, discussed here:

http://www.hibernate.org/hib_docs/v3/re ... child.html

So it looks like the most elegant way of resolving this is to model it as a bi-directional association. I'll probably go ahead and do this, but does anyone have any alternatives? It seems quite heavyweight generating so many tables given the relatively simple object model I'm dealing with.

Thanks,
Adam.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:29 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Even if you make it bi-directional, you will still have to use the code from the previous post.

Another is option is to manage the relation by hand (issue select, insert, update, delete statements). Not recommanded in my opinion.


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.