Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Interceptor Not Working

I have a simple HibernateInterceptor in which basically I want to set a few fields automatically. This interceptor (shown below) extends EmptyInterceptor:

public class EntityAuditInterceptor extends EmptyInterceptor {

    /**
     * The Serial Version UUID.
     */
    private static final long serialVersionUID = 4636626262147801287L;

    /* (non-Javadoc)
     * @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
     */
    public boolean onFlushDirty(Object entity,  Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {

        // doing stuff here

        return false;
    }

    /* (non-Javadoc)
     * @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
     */
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

                // doing stuff here
        return false;
    } 
}

I am wiring using a spring config file as follows:

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="hsqlDbDataSource"/>

        <property name="packagesToScan">
            <list>
                <value>com.dreamteam.lms.**.*</value>
            </list>
        </property>

        <!-- Adding Interceptor here -->
        <property name="entityInterceptor">
            <bean class="com.dreamteam.lms.interceptors.EntityAuditInterceptor"></bean>
        </property>


        <property name="hibernateProperties">
            <props>
                <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
            </props>
        </property>
    </bean>

However, the Interceptor is never reached. Does anyone have any clue? I have also tried adding the following to the transaction manager bean definition as follows:

<property name="entityInterceptor">
        <ref local="entityAuditInterceptor"/>
    </property>
like image 453
Kros Avatar asked Apr 12 '12 23:04

Kros


2 Answers

Ok, just for the record, I solved this problem which turned out to be a stupid mistake from my part.

When I implemented my interceptor that extends EmptyInterceptor I added the method 'onFlushDirty' and so on. So far so good. The problem was that on using my IDE to auto-import the used classes, I ended up importing java.reflect.Type instead of org.hibernate.type.Type by mistake. Hence, I was not really overriding the interceptor method!

I noticed that when I added the @Override interceptor to my method.

Another mystery solved ... :)

like image 64
Kros Avatar answered Sep 21 '22 15:09

Kros


It looks like your Spring XML configuration is correct, so Hibernate should be calling your interceptor methods. However those methods appear to always return false which means that Hibernate will ignore any changes you make.

When you change a value you must return true. For example, this code iterates through all the properties and returns true if and only if a change is made:

public boolean onFlushDirty(Object entity,  Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
    boolean changed = false;
    for (int i = 0; i < propertyNames.length; i++) {
        if ("saveDate".equals(propertyNames[i])) {
            currentState[i] = new Date();
            changed = true;
        }
    }
    return changed;
}
like image 28
gutch Avatar answered Sep 21 '22 15:09

gutch