-->
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: How to avoid "an object was deleted and then re-saved&q
PostPosted: Mon Sep 08, 2003 1:11 am 
Newbie

Joined: Mon Sep 08, 2003 12:51 am
Posts: 19
Hi all,

In my project, I use CASCADE to save, update or delete my objects.

For example,

class Node {
private Set incoming;
private Set outgoing;
public void addIncoming(Connection c);
public void removeIncoming(Connection c)
public void addOutgoing(Connection c);
public void removeOutgoing(Connection c);
}

class Connection {
private Node source;
private Node target;
//getters&setters
}

I wrote the hibernate mapping xml
<class name="Node">
<set name="outgoing" inverse="true" cascade="all-delete-orphan">
<key column="source"/>
<one-to-many class="Connection"/>
</set>
<set name="incomings" inverse="true" cascade="all-delete-orphan">
<key column="target"/>
<one-to-many class="Connection"/>
</set>
</class>

When I do this,
node1.removeIncoming(c1);
node2.addIncoming(c1);
c1.setTarget(node2);

while the objects tree in memory is

root
/ | \
/ | \
node1 node2 node3

all operations on root has been cascaded on it's children

When I call

session.saveOrUpdate(root);

I hope c1 will be saved with target, node2.

But the excetion is throwed:
net.sf.hibernate.HibernateException: Flush during cascade is dangerous - this might occur if an object was deleted and then re-saved by cascade

How to avoid this?

Furthemore,
I wonder whether "cascade=all-delete-orphan" could help me not to call
session.delete?

Best regards!

Jun Huang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 1:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you can't use orphan delete in this kind of situation. Orphan delete means: "when this object is removed from the collection, _delete_it_".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 2:37 am 
Newbie

Joined: Mon Sep 08, 2003 12:51 am
Posts: 19
Thanks a lot, gavin!

But, in fact,

I implement LifeCycle's method -- onDelete(), on class Connection

public boolean onDelete(Session s) throws CallbackException {
//judge if the connection could be deleted

//in my situation, only target changed,
//so the connection should not be deleted, return true
return sourceAttached && targetAttached;
}

That works fine when I change the source of connection.

However, when I change the target of connection, the connection could
not be saved with it's new target. :-(

I will keep on checking my code.

Best regards,

Jun Huang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 3:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ah! Interesting.

That should theoretically work, though I've never tried anything like that.

Still, *somehow* the object is being marked deleted. Are you _certain_ the lifecycle method is called and is returning true?

Step through the code, esp. with Hibernate logging enabled.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2003 2:43 am 
Newbie

Joined: Mon Sep 08, 2003 12:51 am
Posts: 19
quite strange!

I found that if I call session two times, the target will be saved correctly!
(with two different sessions)

Best regards,

Jun Huang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2003 2:53 am 
Newbie

Joined: Mon Sep 08, 2003 12:51 am
Posts: 19
hunj wrote:
quite strange!

I found that if I call session-saveOrUpdate two times, the target will be saved correctly!
(with two different sessions)

Best regards,

Jun Huang


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 04, 2005 12:28 pm 
Newbie

Joined: Wed May 04, 2005 12:24 pm
Posts: 1
I was able to use the Lifecycle interface as mentioned to successfully move a child between parents. The child collection is still mapped as 'all-delete-orphan'.


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.