-->
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.  [ 14 posts ] 
Author Message
 Post subject: Using Lifecycle and onDelete to implement logical delete
PostPosted: Fri Oct 10, 2003 11:37 pm 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
I'm looking at creating a system where all persistent objects have a cleanupDate field, and to delete an item, you just set the cleanup date to the current date. All persistent objects in my system extend a common class and I thought I could just implement something like this for the onDelete() method in that class (which extends Lifecycle):

Code:
    public boolean onDelete(Session aSession) throws CallbackException {
       logger.debug("onDelete() called.");
        this.setCleanupDate(new Date());
        try {
            aSession.saveOrUpdate(this);
        } catch (HibernateException e) {
         throw new CallbackException("Couldn't update cleanup date.", e);
        }
        return VETO;
    }


The onDelete() method is being called, but despite the fact that I'm returning VETO, the delete goes ahead and runs.

Any ideas what I'm missing here?

I'm also examining ways to automatically filter items with cleanup dates in the past when results are returned. I haven't looked into it much, but any pointers to easy hooks that I could take advantage of for this functionality would be helpful.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 9:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This should work.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 10:12 am 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
I'll run it through the debugger and see what's going on.


Top
 Profile  
 
 Post subject: Class level WHERE for filtering
PostPosted: Mon Oct 13, 2003 12:40 am 
Newbie

Joined: Wed Oct 08, 2003 7:42 pm
Posts: 18
The only information I could find was the following, but it only seems to handle part of the problem.

...class level WHERE. This allows for example implementing logical delete (delete using a flag) by having hibernate append


Top
 Profile  
 
 Post subject: Handling Cascades???
PostPosted: Mon Oct 13, 2003 12:43 am 
Newbie

Joined: Wed Oct 08, 2003 7:42 pm
Posts: 18
I am also looking into logical deletes at the moment. Are you looking to handle cascading deletes? I need to be able to do this but have not come accross a nice way as yet (getting hibernate to test for referential integrity as if it were a physical delete etc)

Any ideas would be appreciated,
Scott Buckham


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2003 12:52 am 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
Class level WHERE looks like the trick for handling the loading part of logical deletes.

It doesn't look like the Hibernate plugin for XDoclet supports it so I'll have to look into that (it's the reason I haven't tested it yet).


Top
 Profile  
 
 Post subject: return VETO
PostPosted: Mon Oct 13, 2003 4:28 am 
Newbie

Joined: Wed Oct 08, 2003 7:42 pm
Posts: 18
I've tested code nearly exactly the same as you and while it has avoided a physical remove - all my fields have been set to null except for the removed flag that I have and the id.

The weird thing is that I am cascading (cascade=all) to a set of children that also saveandupdate with the flag changed and VETO; and their properties aren't being updated to null except for the foreign key to the parent (it is, however, a bidirectional relationship??). The removed field is being updated successfully.

I tried also throwing an exception but that does not allow the children to enter the onDelete(..) method to attempt cascading the logical deletes.

-Scott


Top
 Profile  
 
 Post subject: Working
PostPosted: Mon Oct 13, 2003 5:23 am 
Newbie

Joined: Wed Oct 08, 2003 7:42 pm
Posts: 18
my logic in the remove method was bad - i was removing by primary key and didn't bother loading the perisistent object first (as I didn't need to to physically remove). I tried on a data object without bidirectional and it worked. I had to removed the children expliciltly however as the cascading saveOrUpdate gaved me a hibernateException - too dangerouse to flush on cascade... still working on it


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 6:02 pm 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
Getting back to this, the class level WHERE attribute works fine when I query using session.find() but not when I use session.load().

Is there an appropriate approach to leave out logically deleted objects when using load()?

Here's my mapping (package names have been changed to protect the innocent).

Code:
<hibernate-mapping>
    <class name="persist.Client"
        table="client"
        where="(cleanup_date is null or cleanup_date > now())" >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <timestamp
            name="dateModified"
            column="date_modified"
        />

        <property
            name="name"
            type="java.lang.String"
            column="name"
        />

        <many-to-one
            name="contact"
            class="persist.AdminContact"
            cascade="none"
            outer-join="auto"
            column="admin_contact_id"
            not-null="false"
        />

        <property
            name="cleanupDate"
            type="java.util.Date"
            column="cleanup_date"
        />

        <property
            name="dateCreated"
            type="java.util.Date"
            column="date_created"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Client.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The class-level where does not, and will not be used during load(), or when resolving single-point associations (ie. many-to-one).

However, it should be changed to be used when loading collections. It is on my todo list, but is not especially trivial.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 8:08 pm 
Newbie

Joined: Mon Sep 08, 2003 10:06 am
Posts: 14
We already superclass Session, so I guess I'll put the check in there. Theoretically people shouldn't be loading deleted items in that manner anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 10:24 pm 
Newbie

Joined: Mon Sep 27, 2004 11:39 pm
Posts: 4
There is a WHERE property on the BAG element in the mapping file. Does this do what you are after?


Top
 Profile  
 
 Post subject: logical delete
PostPosted: Tue Nov 30, 2004 6:43 pm 
Newbie

Joined: Tue Nov 30, 2004 11:39 am
Posts: 4
Did anyone get logical delete with cascade to work? Instead of physically deleting records, I need to set a column to a particular value. The schema has parent/child relationships.


Any tips, ideas, help would be appreciated.

Thanks,
matetn


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 5:50 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Gavin,

Had you implement the where clause for lazy-loading in Hibernate 2.1.x or only in Hibernate3 ?

Thanks for your answer.
Yann


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