I have used PreInsertEventListener
and PreUpdateEventListener
Event Listener to insert created date and updated date in table.
The problem i am fac
I had created Interceptor extend it with Hibernate EmptyInterceptor.
Override method onSave()
which will be Called when you save an object, the object is not save into database yet and onFlushDirty()
Called when you update an object, the object is not update into database yet.
In this function i have check my method by its name in which i have to set date at the time of created or updated.
Here is sample code of onFlushDirty() method.
public boolean onFlushDirty(Object entity,Serializable id,Object[] currentState,
Object[] previousState,String[] propertyNames, Type[] types)
{
if ( entity instanceof City ) {
for ( int i=0; i < propertyNames.length; i++ ) {
if ( "lastUpdatedOn".equals( propertyNames[i] ) ) {
currentState[i] = new Date();
return true;
}
}
}
return false;
}
Here lastUpdatedOn
is my method name which set updated date of record.
onSave() method :
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
if ( entity instanceof City) {
for ( int i=0; i
Here createdOn
is method to set created date for record.
Extend your POJO class with this interceptor class.