javahibernateinterceptorhibernate-5

Hibernate Interceptor not working after upgrading to Hibernate 5


Earlier my Interceptor code was working fine for Hibernate 3. After I upgraded to Hibernate 5 and made the necessary changes, callback methods like onSave & onFlushDirty stopped working.

Regarding library changes, below Hibernate 3 jars I replaced with Hibernate 5 jars.

Hibernate 3 jars replaced-

Hibernate 5 jars added-

Below is my Interceptor code-

public class CustomInterceptor extends EmptyInterceptor {

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
    System.out.println("onFlushDirty called");
    return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}

@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    System.out.println("onSavecalled");
    return super.onSave(entity, id, state, propertyNames, types);
}
}

If someone can point out what I am missing or any correction that I need to make, it would be very helpful.

Please Note- Entire Application is working smoothly except that Interceptor Callback methods are not getting called.


Solution

  • Could you please try with the following:

    Session session = sessionFactory.withOptions()
                      .interceptor(new CustomInterceptor())
                      .openSession();