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.  [ 1 post ] 
Author Message
 Post subject: Issue with Interceptor during update
PostPosted: Thu Jun 11, 2009 8:49 am 
Newbie

Joined: Tue May 13, 2008 8:02 am
Posts: 1
In my table, I've 2 fields : ADD_PROGRAM, LAST_CHANGE_PROGRAM
I need to fill in these columns with the value 'MyProgram' during insertion and updation accordingly.

I am extending and writing my own interceptor to achieve this.
It is working fine for me during insertions but not during updations.

For updations, I used onFlushDirty. I tried to print the messages on the console. They are printing fine. But in the Database, those columns are not getting updated.

Can anybody please point me towards the solution :

My .hbm file, and my interceptor code is outlined below:

Code:
<hibernate-mapping>
    <class name="HealthSystemWorkList" table="HEALTH_SYSTEM_WORK_LIST"
          dynamic-insert="true" dynamic-update="true" select-before-update="true">
        <id name="workListID" type="java.lang.Long">
            <column name="WORK_LIST_ID" precision="18" scale="0" />
            <generator class="native" />
        </id>

        <property name="addProgram" type="java.lang.String">
            <column name="ADD_PROGRAM" length="50" not-null="true" />
        </property>

        <property name="lastChangeProgram" type="java.lang.String">
            <column name="LAST_CHANGE_PROGRAM" length="50" />
        </property>
   </class>
</hibernate-mapping>




Code:
public class AuditInterceptor extends EmptyInterceptor {

   private static final String ADD_PROGRAM = "addProgram";
    private static final String LAST_CHANGE_PROGRAM = "lastChangeProgram";
   
    public boolean onFlushDirty(Object entity, Serializable id,
            Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
       
            try {
                int properyNamesLength = propertyNames.length;
                for (int i = 0; i < properyNamesLength; i++) {
               if (LAST_CHANGE_PROGRAM.equals(propertyNames[i])) {
                       System.out.println("LAST_CHANGE_PROGRAM :: Before :: " + currentState[i]);
                       currentState[i] = "MyProgram";
                       System.out.println("LAST_CHANGE_PROGRAM :: After :: " + currentState[i]);
                       return true;
               }
                }
            }
            catch (Exception e) {
               e.printStackTrace();
            }
        }
        return false;
    }

    public boolean onSave(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types){
            try {
                int properyNamesLength = propertyNames.length;
                for (int i = 0; i < properyNamesLength; i++) {
               if (ADD_PROGRAM.equals(propertyNames[i])) {
                       state[i] = "MyProgram";
                       tsChanged = true;
                    }
                }
            }
            catch (Exception e) {
            e.printStackTrace();
         }
        }
        return false;
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.