MightyApe MightyApe - 2 months ago 7
Java Question

Hibernate Interceptors - Why is onFlushDirty called after onSave?

The Plan



I am using Hibernate to implement createDate and lastUpdate Timestamps for a little project. I use an EmptyInterceptor and overload the provided methods based on the proposed solution i found here.
The solution works fine unless one little detail. I want to add a column indicating if an object has been updated already. I know I could achieve this by simply comparing if there is a difference in the two created and updated timestamps, but I need to have this field indicating that there was an update.

I use the onSave method which gets called when a new object is stored to set the wasUpdated value to 'N', indicating that there was no update. In the onFlushDirty() method I set this value to 'Y'.

The Problem



I would expext that when I create and persist a new Object, that the createDate and the lastUpdate fields have the same Date, but the wasUpdated field is set to 'N' as there was no update. I only use session.save() in my code, there is no session.update() and also no session.saveOrUpdate(). The logs of Hibernate indicate that there is actually an Update, which sets the wasUpdated value to 'Y'.

What can be the source of this update? Where is it triggered?

Object initialization and persistence



I disabled auto-commit in the hibernate.cfg.xml.

<property name="hibernate.connection.autocommit">false</property>


This is how I create the object:

ExampleObject ex = new ExampleObject();
ex.setValue("TestStringValue");
this.session = HibernateUtil.getSessionFactory().openSession();
this.session.beginTransaction();
this.session.save(ex);
this.session.getTransaction().commit();
this.session.close();


The Interceptor



@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {


if (entity instanceof TimeStamped) {

Date insertDate = new Date();
int indexOfCreateDateColumn = ArrayUtils.indexOf(propertyNames, "createdDate");
int indexOfUpdatedDateColumn = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");

state[indexOfCreateDateColumn] =insertDate;
state[indexOfUpdatedDateColumn] =insertDate;
state[indexOfWasUpdated] ='N';

return true;
}
return false;
}


The second method is for setting the lastUpdatedDate and setting the wasUpdated field to 'Y'.

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types) {

if (entity instanceof TimeStamped) {

int indexOfLastUpdate = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");

currentState[indexOfLastUpdate] = new Date();
currentState[indexOfWasUpdated] ='Y';


return true;
}
return false;
}


HibernateUtil



I use this configuration for the session.

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
try {

Configuration configuration = new Configuration().setInterceptor(new TimeStampInterceptor());
configuration.configure();

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);


} catch (HibernateException he) {
System.err.println("Error creating Session: " + he);
throw new ExceptionInInitializerError(he);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}


Versions



I use Maven and Java 1.7.0_40.

<!--. Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.4.Final</version>
</dependency>

Answer

Solution:

Thanks to the hint proposed by @vp8106 I could work around the problem. As I set the lastUpdatevalue to the same date as the creationDate value during record initialization, I just compare the two dates. If they are identical, then this is the firstupdate where I need to set the update indicator wasUpdatedto 'Y'.

Code snippet:

   @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
                            Object[] previousState, String[] propertyNames, Type[] types) {




    /**
     * Update the lastUpdateDate value and set the wasUpdated flag to 'Y'
     */
    if (entity instanceof TimeStamped) {


        int indexOfLastUpdate = ArrayUtils.indexOf(propertyNames, "lastUpdatedDate");
        int indexOfCreatedDate = ArrayUtils.indexOf(propertyNames, "createdDate");
        int indexOfWasUpdated = ArrayUtils.indexOf(propertyNames, "wasUpdated");

        Date createdDate = (Date) previousState[indexOfCreatedDate];
        Date lastUpdateDate = (Date) currentState[indexOfLastUpdate];


        /**
         * If createdDate equals lastUpdateDate, this is the first update.
         * Set the updated column to Y
         */
        if (createdDate.equals(lastUpdateDate)) {
            logger.warning("This is the first update of the record.");
            currentState[indexOfWasUpdated] = 'Y';
        }
        // set the new date of the update event
        currentState[indexOfLastUpdate] = new Date();


        return true;
    }
    return false;
}
Comments