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