-->
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.  [ 1 post ] 
Author Message
 Post subject: Deleting databse orphans in many-to-many and many-to-one
PostPosted: Thu Aug 03, 2006 5:57 am 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
Hi everyone.

In my application, many relationships are modeled as many-to-one and many-to-many. The problem I am facing right now is deleting the orphans.

Assume we have m:m A:B and m:1 C:B.
What I want is that when I delete A, all orphan Bs (which are referenced neither from As nor from Cs) are deleted as well. Otherwise I'll get a whole lot of unused Bs in the database.

I don't think Hibernate can do it out of the box.
It requires testing all potential references to B.

I currently have to ideas for a solution.
The first is a dirty trick that we can actually try deleting B and if we get an constraint violation exception, just ignore it (oops, someone references the object) and don't do further cascades.

The second idea is to analyze hibernate mapping metadata to detect all possible references for each of the mapped classes. Then, implement a time-scheduled task which first detects all unreferenced objects and then removes them. In my case, I can just let the database cleared once a day.

Do you see any other options?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.