-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can I delete rows from a child table?
PostPosted: Thu Dec 22, 2005 3:15 am 
Newbie

Joined: Fri Dec 16, 2005 12:31 am
Posts: 6
Hello -

I am using Hibernate 2.1.6 and have a one-to-many association as shown below:

ParentClass.java---------- Table: PARENT_CLASS

ParentClass {

Collection childClass;

/**
* @hibernate.set role="children" inverse="false"
* cascade="all-delete-orphan"
* @hibernate.collection-key column="parent_id"
* @hibernate.collection-one-to-many
* class="com.samples.ChildClass
*/
public Collection getChildClass () {
return this.childClass;
}
}


ChildClass.java---------- Table: CHILD_CLASS

ChildClass {

ParentClass parentClass;
int number;

/**
*
* @return
* @hibernate.many-to-one column="parent_id"
* class="com.samples.ParentClass"
*/
public ParentClass getParentClass () {
return this.parentClass;
}
}


I'd like to delete all the rows in the child table where the 'ChildClass.number' attribute is equal to 100.

I tried using a build clause that basically used a where childClass.number = 100 but end up getting the following exception:


HibernateObjectRetrievalFailureException :

"deleted object would be re-saved by cascade (remove deleted object from associations): 12345, of class: com.samples.ChildClass"


What does this mean?

What do I need to include in my XDoclet tag definition for the mapping so that I could execute a single delete?

Quick suggestions will be appreciated!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 5:07 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hi,

Did yo mean you tried
session.createQuery("delete ChildClass where number = :number".setInt("number",100).executeUpdate()


If yes, you may need to try either from a fresh session (so no associations are loaded in that session) or you may use the behaviour of delete-orphan: do a select on all those ChildClass having number=100 and then then, foreach of them, do a
childClass.getParent().getChildclass().remove(childClass);

But last is slower as this may need lots of db queries.


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