-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cascade delete on child with composite-id
PostPosted: Mon Apr 21, 2008 3:31 pm 
Newbie

Joined: Mon Apr 14, 2008 11:55 am
Posts: 4
Hibernate version: 3.2.1

Name and version of the database you are using: Oracle 10g

I have a parent-child relationship modeled as follows:

The parent class:
Code:
   <class
      name="Model"
      table="MODEL"
   >
      <meta attribute="sync-DAO">false</meta>
      <id
         name="id"
         type="integer"
         column="MODEL_ID"
      >
         <generator class="sequence">
            <param name="sequence">model_seq</param>
         </generator>
      </id>

      <property
         name="modelName"
         column="MODEL_NAME"
         type="string"
         not-null="true"
         length="64"
      />
      <property
         name="createDate"
         column="CREATE_DATE"
         type="date"
         not-null="false"
         length="7"
      />
      <many-to-one
         name="project"
         column="PROJECT_NAME"
         class="Project"
         fetch="join"
         not-null="true"
      >
      </many-to-one>
   

      <set name="modelDetails" cascade="all,delete-orphan">
         <key column="MODEL_ID"/>
         <one-to-many class="ModelDetail"/>
      </set>


   </class>   

The child class:
Code:
   <class
      name="ModelDetail"
      table="MODEL_DTL"
   >
      <meta attribute="sync-DAO">false</meta>
      <composite-id name="id" class="ModelDetailPK">
         <key-many-to-one
            name="model"
            class="Model"
            column="MODEL_ID"
         />
         <key-many-to-one
            name="costCode"
            class="CostCode"
            column="COST_CODE_ID"
         />
         <key-many-to-one
            name="optionCode"
            class="OptionCode"
            column="OPTION_CODE_ID"
         />
         <key-property column="QUOTE_NBR" name="quoteNbr" type="integer"/>
         <key-property column="STORE_NBR" name="storeNbr" type="integer"/>
      </composite-id>

      <property
         name="costFactor"
         column="COST_FACTOR"
         type="string"
         not-null="true"
         length="1"
      />
      <property
         name="activeFlag"
         column="ACTIVE_FLAG"
         type="yes_no"
         not-null="true"
         length="1"
      />
      <property
         name="lastUpdateDate"
         column="LAST_UPDATE_DATE"
         type="date"
         not-null="false"
         length="7"
      />
      <property
         name="createDate"
         column="CREATE_DATE"
         type="date"
         not-null="false"
         length="7"
      />

   </class>   


The problem is that when I do a delete on a Model that has a single ModelDetail in its set, an update on the ModelDetail (set MODEL_ID = null) is attempted, which causes a database error.

Code:
Hibernate: update MODEL_DTL set MODEL_ID=null where MODEL_ID=?
2008-04-21 13:21:19,554 [main] WARN - SQL Error: 1407, SQLState: 72000
2008-04-21 13:21:19,554 [main] ERROR - ORA-01407: cannot update ("QMS"."MODEL_DTL"."MODEL_ID") to NULL

2008-04-21 13:21:19,554 [main] WARN - SQL Error: 1407, SQLState: 72000
2008-04-21 13:21:19,554 [main] ERROR - ORA-01407: cannot update ("QMS"."MODEL_DTL"."MODEL_ID") to NULL


I have tried several different O/R mapping strategies to get this working properly (including inverse many-to-one and iterating through the details, deleting each one individually before deleting the model), but have had no success. I have seen this work in simpler relational schemas where the child object does not possess a composite-id PK structure, but I'm confounded by what is happening here.

Are there any recommendations or different mapping strategies I might try that would make hibernate cascade the delete?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 2:21 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Hi,
add inverse="true" to your set-Mapping:

Code:
<set name="modelDetails" cascade="all,delete-orphan" inverse="true">
         <key column="MODEL_ID"/>
         <one-to-many class="ModelDetail"/>
      </set>


Then the children should be deleted without setting their ids to NULL...


Top
 Profile  
 
 Post subject: inverse=true
PostPosted: Tue Apr 22, 2008 10:19 am 
Newbie

Joined: Mon Apr 14, 2008 11:55 am
Posts: 4
Thanks typhos.

I had figured that out later in the day yesterday and forgot to update my post. It worked just as you said it would.


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