-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cascading delete of entities that are no Members
PostPosted: Tue Mar 25, 2008 6:19 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
Hello everybody!

Consider the following situation:

Entity A has a collection of Entity B. But entity A actually doesn't know about that, b/c this relation is just defined in Entity B as a <many-to-one> relation.

But whenever I delete Entity A, I want to cascade the delete command!

How can this be achieved?

My first (and ugly) approach is to define a private field in Entity A:

Code:
private ICollection<B> bs;


which, of course, results in a warning that bs is never assigned.

and then define a property in the A.hbm.xml:

Code:
<set name="bs" access="field" lazy="true" cascade="all">
      <key column="typeId" />
      <one-to-many class="B" />
</set>


How would you solve this problem??

Thx in advance & kind regards
Marks


Top
 Profile  
 
 Post subject: Cascading delete of entities that are no Members
PostPosted: Tue Mar 25, 2008 6:43 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Marks,

I would do exactly what you have done ... but if you also make the field protected (instead of private) I think your compiler warning will disappear.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 6:56 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
When you want to use hibernate for deleting the A's, the easiest and cleanest way is the one you already tried. Alternatives may be implementing ILifecycle on A, using an interceptor in the session or define a cascading delete on the foreign key (if you have one) in the database.

If you have your own data access layer between hibernate and your application, you can do the cascade there, depends on architecture of your app.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 7:44 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
actually there are plenty of layers :)

I also thought about a DeleteA() Method, but as I use a generic Dao that already has Delete(object _obj) I wanted to use this method in the first place.
Anyway, I also like the idea of ILifecycle a lot!

Shouldn't cascade="all" result in deleting all B's ?
Right now the whole thing seems only to work with all-delete-orphan, which kinda conflicts with my transaction management.


Top
 Profile  
 
 Post subject: Cascading delete of entities that are no Members
PostPosted: Tue Mar 25, 2008 7:53 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Marks,

cascade="all" will not delete entity objects in a collection. I believe cascade="all-delete-orphan" is correct in this example. You might also want to add inverse="true" to your set, since the child object has a parent reference.

http://www.hibernate.org/hib_docs/reference/en/html/example-parentchild.html

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 8:53 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Just note, that ILifecycle seems to be obsolete. But I don't know if (and when) it will be removed from hibernate. Besides, if you implement OnDelete on A, then A does know something about B. Not as obviuos as with a private mapping, but from a design point it makes no difference at all.

_________________
--Wolfgang


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