-->
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.  [ 6 posts ] 
Author Message
 Post subject: Auditing using Hibernate Interceptor
PostPosted: Mon Feb 21, 2005 4:37 pm 
Newbie

Joined: Sun Feb 13, 2005 10:52 pm
Posts: 3
Hibernate version: 2.1.5

Our app as a requirement to provide a detailed audit log, ie we want to record the before and after values of any field a user changes. We are trying to be database independant, so database triggers are out of the question.

I tried to implement this using the History Interceptor example on the wiki (http://www.hibernate.org/195.html) However although I get all the appropriate debugging indicating that the interceptor is creating history entries however the history entries are never saved to the database.

I'm wondering if postFlush is too late to add them to the object?

Has anyone got this to work? Is there another way of implementing this type of audit logging?


Top
 Profile  
 
 Post subject: Auditing using Hibernate Interceptor
PostPosted: Thu Mar 03, 2005 6:29 pm 
Newbie

Joined: Thu Mar 03, 2005 5:57 pm
Posts: 1
We are having a similar issue.

The interceptor is working perfectly in the add (onSave) case in which the records are added directly to the object. In the update case hibernate is not saving to the database the entries which are added during postFlush.

Our debugging shows the records have been added by the interceptor but they are not picked up and saved by hibernate.

Any suggestions for solving this problem would be greatly appreciated.

Code:
public void postFlush(Iterator it) throws CallbackException {
      log.debug("Post-Flush");
      while (it.hasNext()) {
         Object obj = it.next();
         if (!(obj instanceof Historizable)) {
            continue;
         }
         Historizable h = (Historizable) obj;
         Set newEntries = (Set) histories.get(h);
         if (newEntries == null) {
            continue;
         }
         h.getHistoryEntries().addAll(newEntries);
         System.out.println("History Entries: ");
         Iterator historiesIterator = h.getHistoryEntries().iterator();
         while(historiesIterator.hasNext()){
            System.out.println(historiesIterator.next());
         }
         
      }
      histories.clear();
   }


[banking-pilot] 2005-03-04 10:31:56,510 DEBUG [com.company.infrastructure.auditing.HistoryInterceptor] - <Post-Flush>
History Entries:
historyid: null
who: jbanker1
what: update
property: dateReceived
historyid: 11
who: jbanker1
what: created
property: null
historyid: null
who: jbanker1
what: update
property: dateEntered
[banking-pilot] 2005-03-04 10:31:56,511 DEBUG [net.sf.hibernate.impl.SessionImpl] - <transaction completion>
[banking-pilot] 2005-03-04 10:31:56,511 DEBUG [net.sf.hibernate.transaction.JDBCTransaction] - <re-enabling autocommit>
[banking-pilot] 2005-03-04 10:31:56,512 DEBUG [net.sf.hibernate.impl.SessionImpl] - <closing session>
[banking-pilot] 2005-03-04 10:31:56,512 DEBUG [net.sf.hibernate.impl.SessionImpl] - <disconnecting session>
[banking-pilot] 2005-03-04 10:31:56,512 DEBUG [net.sf.hibernate.impl.SessionImpl] - <transaction completion>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 6:36 am 
Newbie

Joined: Tue Sep 27, 2005 8:12 am
Posts: 12
I wonder if you are able to solve this issue. I am tring to do the same. It works fine on save, but in case of update, history changes are not saved in database. Any help will be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 10:34 am 
Newbie

Joined: Mon Oct 17, 2005 3:42 pm
Posts: 17
Try creating a new Session on the same connection and explicitely calling save on your history objects. If you're using the thread-local session pattern, this should be pretty easy - get the current session and ask it for its connection and hand that connection to a new session from the same factory.
Using the same session is not supported, although you might be able to get it to work, at least in 3.0.5 (wink)

The reason Hibernate isn't picking up your new/modified history objects is that it's already done with the flush and is no longer traversing the objects in session looking for new changes and transient objects to insert.

Even in other interceptor methods (like the findDirty I'm using in a similar situation) you can't rely on cascade behavior, because the session will be in the middle of a flush and (by definition, now that I think about it) will have already examined the object that it is handing to the interceptor.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 10:47 am 
Newbie

Joined: Tue Sep 27, 2005 8:12 am
Posts: 12
Thanks danch, I will try to go with that.
I also tried listener implementation i.e. org.hibernate.event.PreUpdateEventListener. That also didn’t work. One another issue I found in that is though old (OldState) and new (State) values are present in PreUpdateEvent object but doesn’t give properties names.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 5:21 pm 
Newbie

Joined: Mon Oct 17, 2005 3:42 pm
Posts: 17
You can get property names from the Persister passed in the event.

Code:
event.getPersister().getPropertyNames();
[/code]


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