-->
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: Delete strategy
PostPosted: Mon Feb 19, 2007 8:21 am 
Newbie

Joined: Fri Jan 05, 2007 6:44 am
Posts: 12
What is the best solution in hibernate to mark a relation as deleted?

The behavior I would like to achieve is to call remove on some collection to delete an assocation, but for hiberante in the background to not actually delete the join relationship, but instead to mark a status field as DELETED.

Are triggers/interceptors the best solution ?
Is there a class which can be overridden to achieve this?

JC


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 1:55 pm 
Newbie

Joined: Wed Jan 24, 2007 12:30 pm
Posts: 2
You can do this by using and EventListener that listens on "delete" events.

I've written an example from the top of my head so sorry if it doesn't compile or run, however it should give you a hint.

Basically you can create a subclass DefaultDeleteEventListener, override onDelete(DeleteEvent event) and plug it into your sessionfactory. It makes it a lot easier if you create a superclass or interface to identify objects that should have this behavior, in my example "SoftDeletable".

Code:
/** superclass (or make it an interface if you want) for all your domain classes
* that should be "flagged/soft" deleted when deleted instead of "hard" deleted.
*/
public class SoftDeletable {
    private Boolean deleted;
....
}

/** plug this listener into hibernate (config below) */
public class SoftDeleteEventListener extends DefaultDeleteEventListener
    public void onDelete(DeleteEvent event) {
        Object toDelete = event.getObject();   

        // "soft delete" objects that inherit this behaviour
        if( toDelete instanceof SoftDeletable ) {
            Session session = event.getSession();
           
            // set delete-flag and update object.
            (( toDelete ) SoftDeletable).setDeleted( Boolean.TRUE );
            session.update( toDelete );
        } else {
            // Proceed with normal delete for other objects
            super.onDelete( event );
        }
    }
}

Hibernate configuration
Code:
<hibernate-configuration>
    <session-factory>
        ...
        <event type="delete">
            <listener class="foo.bar.SoftDeleteEventListener"/>
        </event>
    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 2:00 pm 
Newbie

Joined: Fri Jan 05, 2007 6:44 am
Posts: 12
...interesting. I'll take a deeper look into this, thanks.


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.