-->
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: dont delete physically in the db, but flag as DELETED
PostPosted: Wed Nov 15, 2006 8:10 am 
Newbie

Joined: Wed Nov 15, 2006 7:38 am
Posts: 7
All my tables have a column 'status' which can be set to DELETED - since a cannot for some purposes erase the entries from the db.

My approach is when i read objects with hibernate from the database i always specify a where "status!=deleted" clause. this works fine specialy for collections which are read from association-tables e.g. User has n Groups.
User.getGroups() only retrieves the Groups from the table user2group (userId,groupId,status) with status != DELTED.
To set the status to DELETE i have a class UserGroupRelation which just reperesents a sinlge row a the mentioned table.

The problem which now arises when i try to remove a group from a user.

Code:

UserGroupRelation r = ....
r.setStatus(deleted);

User u = r.getUser();
Group g = r.getGroup();

// this is needed by hibernate but unfortunalty it will physically remove the relation from the database
u.getGroups().remove(g);


How can i prevent the phy.delete??
Or is the approach kind of uncommon, stupid??
Is there a best-practice-method to the whole "delete by flag" problem?

Thanks in advance
Jens Zastrow


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 10:42 am 
Newbie

Joined: Wed Nov 15, 2006 10:17 am
Posts: 1
mcbain,

as per my understanding, you have a table called user2group (userId,groupId,status). The table is mapped with the UserGroupRelation class.

Now if you set the "status" to DELETED for any row, it means you have done a soft delete on it. Now while fetching as you are checking "status!=deleted" , the soft deleted rows will never be fetch.

Now the question is:: what do you mean by : "when i try to remove a group from a user ?????"

Do you want to remove the association between the user and group??? if it's so then it means that the user2group table should not have any record for the given user and group.

That is why when u r trying "u.getGroups().remove(g);". It's deleting the record.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 12:30 pm 
Newbie

Joined: Wed Nov 15, 2006 7:38 am
Posts: 7
>Do you want to remove the association between the user and group???
yes, but using a "soft remove" - the entries should be kept in the database for later processing.
But as i understood hibernate needs to be informed of all object changes.
If you do not so, hibernate will still keep the old-collection cached containing still the just "removed" element, no?

Is there a way to force hibernate to reload the whole collection?
Would be a little bit inefficient, but working?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 1:32 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
One possible way is to define an Event Listener (Extending from DefaultDeleteEventListener) and override the
Code:
protected boolean invokeDeleteLifecycle(EventSource session, Object entity, EntityPersister persister)

method. Returning True from this method will veto the delete operation from happening. You also have the actual entity object , so you could actually do the update of setStatus("Deleted") in this interceptor rather than all over the code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 6:42 am 
Newbie

Joined: Wed Nov 15, 2006 7:38 am
Posts: 7
i tried this but it works only for buissines sobjects e.g. User/Group.
The entries from the user2grp table which are mapped in a collection Group.getMembers() have no real entity object.

Is there something similar for collections ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 9:33 am 
Newbie

Joined: Wed Nov 15, 2006 7:38 am
Posts: 7
Finally i found the correct solution :-)

@hibernate.sql-delete value=""

The "sql-delete" can be redefinded for the entitiy-class e.g. Group and for the collections too.
I my case i have to redefine the "delete-all" too oder maybe there is there a way disable the one-shot-delete?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 11:09 am 
Newbie

Joined: Sat Oct 14, 2006 10:49 am
Posts: 3
Location: Quilmes
This is working??.. how do you configure that??... I found a problem in all the solutions.. we do not manage the relations between objects.. let's say... the relational table in a ManyToMany.. how can we manage that?..
Hibernate should implement a way to do this.. There is no Real Solution to this problem.. every solution that i read, is a hack.


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.