-->
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.  [ 12 posts ] 
Author Message
 Post subject: Interceptor for create/update date exploding as null
PostPosted: Thu Dec 14, 2006 2:54 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Hibernate 3.2, MySQL 5.0

Some of my data objects have create/update dates that get automagically populated by an interceptor. I can see the persistent entity getting this information set, but it is still being persisted as null.

Code snippets:

Code:
<hibernate-mapping>
   <class name="UserData" table="user">
      <meta attribute="implements">Auditable</meta>

      <property name="createDate" column="usr_create_date" type="timestamp"/>
      <property name="updateDate" column="usr_update_date" type="timestamp"/>
      <property name="createBy" column="usr_create_by" type="string"/>
      <property name="updateBy" column="usr_update_by" type="string"/>
   </class>
</hibernate-mapping>



public interface Auditable extends Persistable {
   Date getCreateDate();
   Date getUpdateDate();
   String getCreateBy();
   String getUpdateBy();

   void setCreateDate( Date createDate );
   void setUpdateDate( Date updateDate );
   void setCreateBy( String createBy );
   void setUpdateBy( String updateBy );
}



class AuditInterceptor extends EmptyInterceptor {
   protected void audit( Object entity, Object[] state ) {
      if( entity instanceof Auditable ) {
         Auditable data = (Auditable) entity;
      
         //New audit info, create from scratch
         Date now = new Date();
         if( data.getCreateDate() == null ) {
            data.setCreateDate( now );
            data.setCreateBy( "this guy" );
         }
      
         data.setUpdateDate( now );
         data.setUpdateBy( "this guy" );
      }
   }

   
   public boolean onFlushDirty( Object entity, Serializable id, Object[] currentState,
                         Object[] previousState, String[] propertyNames, Type[] types )
                                                      throws CallbackException {
      audit( entity, currentState );
      return super.onFlushDirty( entity, id, currentState, previousState, propertyNames, types );
   }

   
   
   public boolean onSave( Object entity, Serializable id, Object[] state, String[] propertyNames,
                     Type[] types ) throws CallbackException {
      audit( entity, state );
      return super.onSave( entity, id, state, propertyNames, types );
   }
}


//Some other class
Object executeFullTransaction( Object data ) throws Exception {
      Object o = null;

      Transaction transaction = null;
      Session session = null;
      try {
         session = factory.openSession( new AuditInterceptor() );
         transaction = session.beginTransaction();

         session.saveOrUpdate( data )

         session.flush();
         transaction.commit();
      } catch( Exception e ) {
         if( transaction != null ) {
            try {
               transaction.rollback();
            } catch( HibernateException rollbackerror ) {
               log.warn( rollbackerror, rollbackerror );   //Can't do nothin'...
            }
         }

         throw e;
      } finally {
         try {
            session.close();
         } catch( HibernateException e ) {
            log.warn( e, e );
         }
      }

      return o;
   }


Now, in the debugger, I can see that audit() is definately firing and that the changes are happening in the entity object. But during actual persistence, the dates and bys are null. These code snippets are pasted and modified...ignore syntactical mistakes.

My data objects are auto-generated...Any reason why the persistent object could get a date parameter set and still have it persist as null? I'm boggled.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 9:03 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
[url]http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Interceptor.html#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])[/url]

Return

true
if the user modified the currentState in any way

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 12:14 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Dang, I missed that (Hibernate 2 code returned false for both and it was working).

I've set onFlush() and onSave() to both return true, but the changes still aren't being included in the actual insert SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 12:15 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
[Double post]


Last edited by Tremelune on Tue Dec 19, 2006 12:19 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 12:17 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
[Double post]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 12:40 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Tremelune wrote:
Dang, I missed that (Hibernate 2 code returned false for both and it was working).

I've set onFlush() and onSave() to both return true, but the changes still aren't being included in the actual insert SQL.


I dont see your code making changes to the "state" its just modifing the entity .. can you use a listener istead

http://forum.hibernate.org/viewtopic.php?t=968660

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 1:10 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
You are correct--I'm only modifying the entity in this case.

Listeners appear to be created on a per-factory basis...I need a way to pass in a parameter per-session (in this case, a String). With the interceptor, I would just open sessions with a new interceptor constructed with a passed-in String. I can't figure out a way to do that with Listeners...

I also can't figure out why the Interceptor isn't working in the first place...doh...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 5:17 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Can anyone find an issue with my interceptor? Or does anyone know how to use per-session listeners?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:33 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Tremelune wrote:
Can anyone find an issue with my interceptor? Or does anyone know how to use per-session listeners?


wondering if you can create different session factories one with ur listener and one without

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:24 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
The Listener object would need to change for every session, though. Not just one or the other.

Maybe I'm going about this all wrong. All I'm trying to do is audit updates on rows. Create date, update date, and just as important, create by and update by.

The by would be the username of the logged in user, so depending on who is logged in, that string would need to get passed in somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 1:15 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
How does anyone track auditing changes if not like this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 1:46 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Tremelune wrote:
How does anyone track auditing changes if not like this?


not sure if this is exactly what you are looking for but there is a
similar example in

http://www.hibernate.org/400.html

called

AuditLogInterceptor

and there are few more examples in wiki area

http://www.hibernate.org/318.html
http://www.hibernate.org/195.html

_________________
Don't forget to rate


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