-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best solution
PostPosted: Thu Dec 16, 2004 12:48 pm 
Newbie

Joined: Thu Jul 08, 2004 1:10 pm
Posts: 11
Hello, good afternoon.

In my web application, every table has two final columns: "LastModificationUser", "LastModificationTime".
They register the name of the user (stored in the session) and the time of the last update/insert in that row.

I´m sure that there are better ways to update these properties instead of start adding the same code in every update/insert method in my application.

I know that this can be done with AOP, but I´m just not a fan.

So I wondered if this can be achieved using Hibernate Interceptors, but I´m not sure how to do it. What should I be looking for?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 16, 2004 2:51 pm 
Newbie

Joined: Thu Jul 08, 2004 1:10 pm
Posts: 11
Just read the faq, and the solution became quite simple:

Code:
   public boolean onSave( Object entity, Serializable id, Object[] state,
         String[] propertyNames, Type[] types ) throws CallbackException
   {
      return setModificationUser( propertyNames, state );
   }

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

   public boolean setModificationUser( String[] propertyNames,
         Object[] currentState )
   {
      boolean dirty = false;

      for( int i = 0; i < propertyNames.length; i++ )
      {
         if( "user".equals( propertyNames[ i ] ) )
         {
            currentState[ i ] = user;
            dirty = true;
         }
         if( "ua".equals( propertyNames[ i ] ) )
         {
            currentState[ i ] = new SimpleDateFormat( "yyyy-MM-dd HH:mm" ).format( new Date() );
            dirty = true;
         }
      }

      return dirty;
   }


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