升级到 Hibernate 5 后 Hibernate Interceptor 不工作

Hibernate Interceptor not working after upgrading to Hibernate 5

早些时候,我的拦截器代码在 Hibernate 3 上运行良好。在我升级到 Hibernate 5 并进行必要的更改后,回调方法(如 onSave 和 onFlushDirty)停止工作。

关于库更改,我将 Hibernate 3 jar 以下替换为 Hibernate 5 jar。

替换了 Hibernate 3 jar-

添加了 Hibernate 5 jar-

下面是我的拦截器代码-

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

如果有人能指出我遗漏的地方或我需要做的任何更正,那将非常有帮助。

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

能否请您尝试以下操作:

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