Hibernate EmptyInterceptor onFlushDirty() 没有执行

Hibernate EmptyInterceptor onFlushDirty() is not executing

我想对我的实体更新进行审计。所以我实现了 EmptyInterceptor.
我的 onFlushDirty() 方法没有执行,但是 afterTransactionCompletion() 确实执行了

我正在使用 Spring 4.1 和 Hibernate 5.0
+
除了@Component配置文件,我还没有做任何配置,直到afterTransactionCompletion()被执行。

我在这里缺少什么?
另外如何拦截事件 query.executeUpdate() ?

我的拦截器class如下:

@Component
public class AuditLogInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean onFlushDirty(Object entity, Serializable id,
            Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        System.out.println("AuditLogInterceptor.onFlushDirty()");
        System.out.println();
        System.out.println("Property Names :-  ");
        for (String propertyName : propertyNames) {
            System.out.print(", "+propertyName);
        }
        System.out.println();
        System.out.println("Current State :-  ");
        for (Object current : currentState) {
            System.out.print(", "+ String.valueOf( current ) );
        }
        System.out.println();
        System.out.println("Previous State :-  ");
        for (Object previous : previousState) {
            System.out.print(", "+ String.valueOf( previous ) );
        }
        return true;
        //return super.onFlushDirty(entity, id, currentState, previousState,
                //propertyNames, types);
    }
    @Override
    public void afterTransactionCompletion(Transaction tx) {
        // TODO Auto-generated method stub
        System.out.println("AuditLogInterceptor.afterTransactionCompletion()");
        super.afterTransactionCompletion(tx);
    }
    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types) {
        System.out.println("AuditLogInterceptor.onSave()");
        System.out.println("Property Names :-  "+Arrays.toString(propertyNames));
        System.out.println("States :-  "+Arrays.toString(state));
        return super.onSave(entity, id, state, propertyNames, types);
    }
    @Override
    public void postFlush(@SuppressWarnings("rawtypes") Iterator entities) {
        System.out.println();
        System.out.println("AuditLogInterceptor.postFlush()");
        for ( ; entities.hasNext() ;) {
            System.out.println("-----"+ entities.next().getClass().getSimpleName());
        }
        System.out.println();
        super.postFlush(entities);
    }
}

我的 DAO 中的代码

@Override
public boolean updateAssignment( Integer workTaskDetailId, short workTaskStatus ) {
    Session session = null;
    Transaction transaction = null;
    boolean isUpdated = false;
    try {
        session = this.sessionFactory.withOptions().interceptor( new AuditLogInterceptor() ).openSession();
        transaction = session.beginTransaction();
        String COMPLETION_DATE = ""; 
        if( workTaskStatus == 263 )
            COMPLETION_DATE = ", wtd.completionDate = :completionDate "; 
        final String UPDATE_WORKTASKSTATUS = "update WorkTaskDetail wtd set wtd.workTaskStatus = :workTaskStatus "
                                            +COMPLETION_DATE+ "where wtd.workTaskDetailId = :workTaskDetailId ";
        Query query = session.createQuery(UPDATE_WORKTASKSTATUS).setShort("workTaskStatus", workTaskStatus)
                .setInteger("workTaskDetailId", workTaskDetailId);
        if( workTaskStatus == 263 )
            query.setDate("completionDate", new Date() );
        int updateCount = query.executeUpdate();
        if( updateCount > 0 )
            isUpdated = true;
        if( session != null )
            session.flush();
        if( transaction != null && transaction.getStatus().equals(TransactionStatus.ACTIVE) )
            transaction.commit();
    } catch ( Exception exception ) {
        if( transaction != null && transaction.getStatus().equals( TransactionStatus.ACTIVE) )
            transaction.rollback();
        LOGGER.error("Message     :- "+exception.getMessage());
        LOGGER.error("Root Cause  :- "+exception.getCause());
        LOGGER.error("                 ************************************************************");
    } finally {
        if( session != null )
            session.close();
    }
    return isUpdated;
}

未调用 onFlushDirty 方法,因为当前 运行 持久性上下文没有修改任何实体。

只有托管实体可以生成自动更新语句。在您的情况下,您正在执行手动 SQL 更新,这超出了 Hibernate 实体状态转换的范围。

因此,您有两个选择:

  1. 使用 Hibernate Envers,因为无需在 Hibernate 之上编写自制审计日志。
  2. 使用 Debezium,因为数据库已经具有审计日志(a.k.a。WAL、重做日志、事务日志)。