-->
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.  [ 5 posts ] 
Author Message
 Post subject: Delete entities without delete
PostPosted: Sun Feb 21, 2010 6:47 am 
Newbie

Joined: Sun Feb 21, 2010 5:29 am
Posts: 3
Hi everybody, need your help please...
My purpose is to delete entities with Hibernate without actually delete those entities from database. I.e., to mark those entities as deleted (e.g. using Boolean flag ‘isDeleted’ within all entities base class). Moreover, I would like to take advantage of the cascading power built in Hibernate so any entity that is being deleted will cascade the operation to its children (“customer” and collection of “orders”: “orders” should be deleted too)
After 2 days (!) searching the WEB for an elegant solution, I realized that I need to implement and register the Delete event listener. And so I did.
First thing I have learned is that using Interceptors cannot help me here. Those are called from within the event (in my case is delete), letting the user a chance to do something (e.g. update) but the deletion is going to be done anyway. Next…
So, I override the onDelete method of DefaultDeleteEventListener class trying to modify it so deletion will not occur. I have noticed that the deletion is done by deleteEntity method. Overriding that method seems to do what I’m looking for but this method cannot be override (declared as final).
Trying to override invokeDeleteLifecycle function (the only one that returns Boolean) and return ‘true’ instead of ‘false’ did the job but without cascading (the ‘cascading’ stopped immediately when returning ‘true’ from the function).
I have also tried to clean the ActionQueue (within the event), but its clear method clears everything (not just the deletion list, so any desired update is cleared too)
I was trying also to override any Flush event. Any modification just makes me more depressed…
My Question: Is there a way to do what I am looking for??? (Deletion of entities without actually delete, just mark it as deleted and very very important – do the “delete” cascading either).
BTW: We are using Hibernate 3.3.2 (not using Entity Manager), jBoss 4.x
Any idea/suggestion is appreciated!
Simon (the frustrated)


Top
 Profile  
 
 Post subject: Re: Delete entities without delete
PostPosted: Sun Feb 21, 2010 10:24 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
Hi what you are trying to do is disabling the records(including cascade disable).

So basically when the client calls delete customer,
you are going to do the following.
....
customer.setDisabled(true);
for(Order order : customer.getOrders())
{
order.setDisabled(true);
}
....
Hibernate point of view what you are doing is cascade update.
If you have enabled cascade="save-update" on customer, following call will update the orders as well.
...
session.saveOrUpdate(customer);

since you don't want to delete the records, you will never call the session.delete()


Top
 Profile  
 
 Post subject: Re: Delete entities without delete
PostPosted: Mon Feb 22, 2010 1:31 am 
Newbie

Joined: Sun Feb 21, 2010 5:29 am
Posts: 3
Hi, thanks for ur reply... :-)
If I understood you correctly, you encourage me not using delete operation but to do an update operation taking into an advantage the “save-update” cascading updating of Hibernate.
But:
1. How the “save-update” in the parent entity will update its children? Suppose I want to delete a “customer” (the parent) and therefore all its “orders” (its children). You say – mark the parent as deleted (setDisabled(true)) and all the children will be updated as well. But how? Modifying the flag in the parent side will not modify all children’s deleted flag.
Am I wrong? (Of course, looping through the children and perhaps each child that has list of children either is not an option)
2. Less crucial: Implementing “delete” as an “update” operation will not “catch” hbm queries like “delete from ….. where…” (but I promise not using that if you have a solution for number 1 :-) )

Any idea?
Thanks for any response...


Top
 Profile  
 
 Post subject: Re: Delete entities without delete
PostPosted: Mon Feb 22, 2010 6:57 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
cascade "save-update" won't do that. It is simply reduces the lines of code.

Without cascading you will do the following.
1)session.saveOrUpdate(customer);
2)for(Order order: cutomer.getOrders()) session.saveOrUpdate(order);

with cascade "save-update", it becomes as below
1)session.saveOrUpdate(customer);

But if you don't set the disabled property of order, it is not going to get disabled.

why don't you do this in your Customer class:

public class Customer {
....
private Collection<Order> orders;
.....

public void setDisabled(boolean disabled)
{
this.disabled=disabled;
for(Order order : this.getOrders())
order.setDisabled(true);
}

}


Top
 Profile  
 
 Post subject: Re: Delete entities without delete
PostPosted: Wed Feb 24, 2010 4:07 am 
Newbie

Joined: Sun Feb 21, 2010 5:29 am
Posts: 3
Hi and thanks again for your concern - BUT your solution it not what I had in my mind ;-)
Your solution requires a lot of work and maintenance: for each entity that has a list of children, I need to write an extra code that iterates its children and perhaps those children have some children too.
Moreover, if I'm modifying the structure of my entities (database), e.g. adding list of children (one-to-many) to an entity, then I need to modify the "delete()" code of the parent either. This is the maintenance I'm talking about.
Using Hibernate event handler, I get it for free (all the cascading is done automatically) without worry that I forgot something...
What do you say?

Anyhow, THANK YOU!

I will continue struggling... :-(


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