-->
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: Parent/Child - Delete Node But don't include descendants
PostPosted: Fri Aug 13, 2004 12:29 pm 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
Hi,

Is it possible to remove a node from a hierarchy of entities (parent/child relation) without remove its descendants node?.

If he decides to include descendants (so remove the whole branch), cascade="all-delete-orphan" is perfect (my mappings are OK, no reason to post them here)

A->B->C

If B is removed [A.removeChild( B );], cascading removes C

However, if the user deletes a node but don't want to include descendants, I thought I could do:

A.removeChild( B );
A.addChild( C );

But it doesn't work.

I'm currently tracing the Hibernate code/log to see what is the problem.

Let me know if you have any idea how I can do this.

_________________
- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 12:52 pm 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Hi.

Tried mapping something like cascade="save-update" or cascade="none" between B and C?

/C


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 12:52 pm 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
When I try to remove B in

A->B->C without including descendants (C), by doing

A.removeChild( B );
A.addChild( C );

when the commit occurs:

B and C are deleted (cascade)

Throw when trying to add B to A:

Caused by: net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for com.convera.repository.model.gurl.LexicalSetPredicate instance with identifier: 1071

Which is comprehensible.

So, why is C deleted from the DB even if it is not really an orphan (associated to a new parent) ?

Thank you. I really need this to work. If I have to do things manually, just tell me!

_________________
- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 12:56 pm 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
kantorn

That would probably work, but I would lose the fact I can delete a whole branch by having cascading in action.

That would also mean I would have to delete my object manually (session.delete( object )) since orphans will not be automatically deleted by hibernate (when using the all-delete-orphan cascade style).

But If I have to do things manually, I will. But as written in my previous post, I don't understand why the deleted child entity is considered an orphan since it is now associated to a new parent (and this before the actual commit!)

_________________
- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 14, 2004 1:32 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
I think this behaviour makes sense, really. StaleObjectException has to mean that Hibernate has "marked" C for deletion or something like that, and after that it's not possible to handle it without exceptions. We'll just have to accept this, and there probably is a good reason for it.

Wouldn


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 4:20 am 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
Hey,

Yes cloning would work, but that means hibernate will have to delete and reinsert C and all its descendants.

I found a solution!

For A->B->C, suppose you delete B:

Code:

[begin transaction]

// optimization, if we know theres no descendants, we dont have to flush and refresh
if ( B.getChildCount() > 0 ) {
      
    for each child of B {

        A.add( child );

    }

    /* the flush will update all children of C to now point to A (update instead of insert since the entities are already persistent) */
    session.flush();

    // we refresh B since it dont have children anymore
    session.refresh( B );

    // we can now remove B, it has no more descendants
    A.removeChild( B );

[commit transaction]
}



The in-memory domain model, the persistence store and the hibernate cache are all synchronized and reflects what is wanted:

A->C

The only problem is that I can't put this piece of code in the domain entity. Even if that's domain logic, since I access hibernate (the persistence layer). I would be coupling the domain model (pojos) with the persitence layer. So I created a command that take care of this.

_________________
- Frank


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.