-->
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: delete-orphan updates child reference, doesn't delete child
PostPosted: Mon Aug 04, 2008 4:42 am 
Newbie

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

I am having a serious problem that I just can't solve. I have a parent object with multiple children. I have updated the classnames in this example to protect our project but the idea is the same. Here are the mappings.

Parent mapping contains:
Code:
<set name="childrenSet" cascade="delete-orphan">
  <key>
  <column name="PARENT_ID" precision="15" scale="0" not-null="true" />
  </key>
  <one-to-many class="com.test.model.Child" />
</set>


Child mapping contains:
Code:
<many-to-one name="parentObj" class="com.test.model.Parent" update="false" insert="false" fetch="select">
  <column name="PARENT_ID" precision="15" scale="0" not-null="true" />
</many-to-one>


In my code I execute the following:
getHibernateTemplate().delete( parent );

In this example the parent object is an instance of a parent object loaded by hibernate.

When the code is executed I get an error stating that a Child row cannot be update because PARENT_ID cant be set to null. It seams as if Hibernate is trying to detach the parent from the child by setting the reference to null instead of deleting the child entirely. If this is true, isnt this a bug in hibernate?

Thanks in advance,
Martyn


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 5:13 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Have you tried with cascade="delete"? I think the delete-orphan option is useful only when you are removing child elements from the parent set. Eg. parent.getChildrenSet().remove(child).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 5:49 am 
Newbie

Joined: Fri Mar 23, 2007 4:47 am
Posts: 16
I tried your solution but unfortuntly it doesn't work. I still get the following error:

ORA-01407: cannot update ("DBUSER"."CHILD"."PARENT_ID") to NULL


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 7:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I checked my own (almost identical) mapping again. It turns out that I have inverse="true" also on the <set>:

Code:
<set
       name="helpTexts"
       lazy="true"
       inverse="true"
       cascade="delete"
     >


And indeed, if I remove the inverse="true" attribute I get a similar error (I am using MySQL):

Code:
Data truncation: Column set to default value; NULL supplied to NOT NULL column 'client_id' at row 1


So, the solution would be to have: cascade="delete" inverse="true"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 10:13 am 
Newbie

Joined: Fri Mar 23, 2007 4:47 am
Posts: 16
Thanks, I tried that but now I'm getting another error:

ORA-02292: integrity constraint (DBUSER.MULTIPLE_CHILD_R1) violated - child record found

I examined the database and MULTIPLE_CHILD_R1 is the foreigen key name for the child to parent relation. So PARENT_ID column has a foreign key that points to the unique id of the parent.

The mapping child 2 parent mapping looks like this:
Code:
<many-to-one name="parentObj" class="com.test.model.Parent" update="false" insert="false" fetch="select">
  <column name="PARENT_ID" precision="15" scale="0" not-null="true" />
</many-to-one>


Martyn


Top
 Profile  
 
 Post subject: Cascading merge and delete issues...
PostPosted: Mon Oct 27, 2008 12:22 pm 
Newbie

Joined: Tue Oct 14, 2008 6:30 pm
Posts: 1
Hi all,

I'm also having the same problem with cascades on merges and deletes with a table having a many to one, one to many relationship. Did any of you happen to figure out why hibernate NULLs out fields on the entity that are marked as "nullable=false"? This problem has been bothering me for quite some time now.

Thanks,
Wade


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.