-->
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.  [ 7 posts ] 
Author Message
 Post subject: cascade="all-delete-orphan" is not working
PostPosted: Mon May 05, 2008 5:02 pm 
Newbie

Joined: Sat May 03, 2008 12:30 pm
Posts: 9
I have modelled a question object with one to many relationship with Answer objects.

I have used cascade="all-delete-orphan" to ensure that whenever the parent (Quesiton) is deleted , all the associated Children (Answer)should be deleted. For some reason the children are not deleted. Can you please take a look at it?

Hibernate version:3

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2008 11:13:26 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.survey.model.SurveyQuestion" table="SurveyQuestion" >
<id name="questionId" type="big_decimal">
<column name="QUESTION_ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="SurveyQuestionTypes" class="com.survey.SurveyQuestionTypes" fetch="select">
<column name="QUESTION_TYPE" length="20" />
</many-to-one>
<property name="status" type="string">
<column name="STATUS" length="20" />
</property>
<property name="text" type="string">
<column name="TEXT" length="1000" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="20" />
</property>
<property name="creationDate" type="timestamp">
<column name="CREATION_DATE" length="11" />
</property>
<property name="lastUpdatedBy" type="string">
<column name="LAST_UPDATED_BY" length="20" />
</property>
<property name="lastUpdatedDate" type="timestamp">
<column name="LAST_UPDATED_DATE" length="11" />
</property>
<property name="altText" type="string">
<column name="ALT_TEXT" length="1000" />
</property>
<set name="surveyAnswers" inverse="true" cascade="all,delete-orphan">
<key>
<column name="QUESTION_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.survey.model.SurveyAnswer" />
</set>
<set name="surveyQuestionSets" inverse="true" cascade="all,delete-orphan">
<key>
<column name="QUESTION_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.survey.model.SurveyQuestionSet" />
</set>
</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Session session = null;
try{
session = this.sessionFactory.getCurrentSession();
Transaction txn=session.beginTransaction();

//delete the question
SurveyQuestionDao surveyQuestionDao = new surveyQuestionDao ;
surveyQuestionDao.deleteQuestion(question,session);

txn.commit();
// return result;
return 1;
}catch(Exception e)

Full stack trace of any exception that occurs:

Name and version of the database you are using:
oracle 9i

The generated SQL (show_sql=true):
delete from SURVEY_QUESTION where question_id=?

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject: Re: cascade="all-delete-orphan" is not working
PostPosted: Mon May 05, 2008 5:33 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What is inside deleteQuestion?


Farzad-


Top
 Profile  
 
 Post subject: suggestion
PostPosted: Tue May 06, 2008 7:06 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I think that you are supposed to first attach your question object to the session, if you want any changes to its children to take effect during commit. correct? Otherwise, you have to call session.save() or merge(), depending on what you're trying to achieve...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 11:10 am 
Newbie

Joined: Sat May 03, 2008 12:30 pm
Posts: 9
This is the namedquery I am using inside deleteQuestion

delete from SurveyQuestion question where question.questionId=:questionId

Thanks for your time.


Top
 Profile  
 
 Post subject: question
PostPosted: Tue May 06, 2008 11:29 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
did you make sure that your DDL is generated with ON DELETE CASCADE for the foreign key between answer and question? AFAIK, bulk-delete will only work as expected if the DDL has this declared.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 12:25 pm 
Newbie

Joined: Sat May 03, 2008 12:30 pm
Posts: 9
The DDL reads as ON DELETE SET NULL which exactly is happening. It is a legacy database and I cannot change that.

Will the database cascade instruction takes priority over the hibernate specification?

Thanks for your time.


Top
 Profile  
 
 Post subject: no precedence
PostPosted: Wed May 07, 2008 4:14 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Not exactly. When hibernate generates an SQL query that relies on database cascading, the db cascading will be used. Sometimes, hibernate does not utilize db cascading, i.e. it manually generates multiple 'delete' queries in the correct order, to delete a certain entity and its dependencies. However, this is usually not supported for bulk-operations. Try this: instead of deleting using a bulk-operation, use the hibernate session delete/remove method and see if it makes any difference for a single entity.


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