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: cascade delete only cascades when loaded in current session
PostPosted: Tue Aug 12, 2008 3:38 am 
Newbie

Joined: Fri Mar 23, 2007 4:47 am
Posts: 16
Hi All,

I have discovered something with Hibernate and I need to know if it's a bug or just the way Hibernate works.

I have a parent/child situation. I have mapped the parent so that the children are a Set and I have set cascade="delete". If I have a parent object that I try to delete using
Code:
getHibernateTemplate().delete( parent )

I get an error saying that the parent still has children. That is very strange because I have set cascade to delete.

I then try to it differently. Before I call delete I try to load the object from the database using a detached criteria.

Code:
Parent parent = hibernateTemplate.findByCriteria( detachedCriteria ).get( 0 );
parent.getChildren().size();
getHibernateTemplate().delete( parent );


Now it works. It seams that the object can only delete it's children if the object was loaded in the session. Isnt hibernate just supposed to say

DELETE FROM Children WHERE Parent_id=parentId
and then
DELETE FROM Parent WHERE Id=parentId

no matter if the object has been loaded in the session?

Thanks in advance,
Martyn


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.