gpt4 book ai didi

java - Hibernate EmptyInterceptor onFlushDirty() 没有执行

转载 作者:行者123 更新时间:2023-12-05 05:19:44 25 4
gpt4 key购买 nike

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

我正在使用 Spring 4.1 和 Hibernate 5.0
+
我没有做任何配置,而不是 @Component 在配置文件中直到 afterTransactionCompletion() 被执行.

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

我的拦截器类如下:

@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 方法没有被调用,因为没有实体被当前运行的 Persistence Context 修改。

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

因此,您有两个选择:

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

关于java - Hibernate EmptyInterceptor onFlushDirty() 没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com