-->
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.  [ 4 posts ] 
Author Message
 Post subject: previous-/currentState of collections in onFlushDirty
PostPosted: Fri Sep 26, 2003 11:41 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
If a property, thats a Set is changed by Set.add() or Set.remove() and Interceptor.onFlushDirty() is called, then the previousState and the currentState of this property are the same object Set. So I can't really get the previousState and the currentState of the Collection.

Does anybody know a way to get this information of the collection in Interceptor.onFlushDirty()?

Thanks in advance


Top
 Profile  
 
 Post subject: Auditing collection changes
PostPosted: Fri Sep 26, 2003 12:00 pm 
Newbie

Joined: Fri Sep 26, 2003 11:44 am
Posts: 13
Hello,

Since I have had to do this, I thought I would give you my insight. There are several issues with dealing with collections in the onFlushDirty interceptor method:

1. If only a collection property has changed for an object the interceptor method is not called. I had to patch Hibernate to get around this problem.

2. As you mentioned it is not possible to determine differenences between the previous and old state using the previous and current state variables because each one will point to the same collection if the collection reference has not been changed.

3. You can retrieve differences between the old collection and new, by using some of the collection wrapper methods. Some of the wrappers have functions like needsInserting and getDeletes which can give you what you want, but sometimes what you really need to do is get the internal collection snapshot in the wrapper. Unfortunately the method is protected, but since I had a case where I needed to get at it, I just patched hibernate to make the method public. However, I would be remiss in not saying that depending on these internal functions is very risky and probably a bad idea because they are not really part of any Hibernate public interface. I have toyed with the idea with patching Hibernate to have a sort of CollectionAuditMetaData api to allow this to work better.

3a. You have to be very careful when using these internal functions because they assume the collection reference has not changed. What I did was to enforce this fact was make my collection getters private and create another function which copied over elements to the existing collection reference.

3b. To be very precise in detecting collection changes that Hibernate will make, it is best to use the needInserting and getDeletes functions instead of using the snapshot because testing for equality is not as straightforward as you might think.

Hope that helps,

Regards,

_________________
Ali Ibrahim


Top
 Profile  
 
 Post subject: list / map
PostPosted: Thu Nov 13, 2003 5:25 am 
Newbie

Joined: Tue Oct 28, 2003 8:00 am
Posts: 16
Location: Karlsruhe, Germany
hi,

can I use the list or map interface on the element of currentState and previousState and read the elements of the list or map so I can check differences ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 8:20 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
Only if the objects are initialized and not lazy any more!

But you only get differences in previous-/currentState, if the collection object itself has changed. Otherwise previous-/currentState references to the same object id!

So, if you want to get differences, it's enough to check
Code:
previousState != currentState

and if this is true, the collection is not lazy and you can get the elements.

If
Code:
previousState == currentState
than there's no way to get the information, if the elements of the collection have changed, at the moment.

For further details check the Forum by searching for "Interceptor onFlushDirty"


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