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;
}
}