Java 在hibernate';中更新之前获取原始对象;s实体侦听器
我试图实现一个entitylistener,它在Java 在hibernate';中更新之前获取原始对象;s实体侦听器,java,spring,hibernate,spring-boot,entitylisteners,Java,Spring,Hibernate,Spring Boot,Entitylisteners,我试图实现一个entitylistener,它在@PreUpdate事件中比较原始对象和更新对象。我尝试使用Jpa存储库从DB中按Id查询原始对象,以将其与事件中接收到的对象进行比较。但是它检索相同的对象(我猜它返回会话范围中存在的对象) 是否有办法从实体侦听器中的数据库获取原始对象?JPA不提供EntityListener中的新旧状态 但您可以使用Hibernates拦截器: public static class LoggingInterceptor extends EmptyInterce
@PreUpdate
事件中比较原始对象和更新对象。我尝试使用Jpa存储库从DB中按Id查询原始对象,以将其与事件中接收到的对象进行比较。但是它检索相同的对象(我猜它返回会话范围中存在的对象)
是否有办法从实体侦听器中的数据库获取原始对象?JPA不提供EntityListener中的新旧状态 但您可以使用Hibernates拦截器:
public static class LoggingInterceptor extends EmptyInterceptor {
@Override
public boolean onFlushDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
LOGGER.debugv( "Entity {0}#{1} changed from {2} to {3}",
entity.getClass().getSimpleName(),
id,
Arrays.toString( previousState ),
Arrays.toString( currentState )
);
return super.onFlushDirty( entity, id, currentState,
previousState, propertyNames, types
);
}
}
在本例中,您可以看到将当前和以前的状态作为易于比较的数组
请阅读文档:这篇文章可能会帮助您: