onFlushDirty()的先前状态对象 [英] previous state object of onFlushDirty()
问题描述
如何在EmptyInterceptor的onFlushDirty()方法中获取实体的先前状态对象?我可以拥有当前会话和当前实体对象,但是不会获得该实体对象的先前状态.请帮助
How to get the previous state object of an entity in onFlushDirty() method of EmptyInterceptor? I can have the current session and current entity object but getting the previous state for that entity object is not coming up. Please help
推荐答案
您可以使用Object[] previousState and String[] propertyNames
参数访问先前的对象值.
You can access previous object values using Object[] previousState and String[] propertyNames
parameters.
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types) {
//Assume SampleBO is your BO class
if (entity instanceof SampleBO){
String userName= previousState[ArrayUtils.indexOf(propertyNames, "userName")].toString();
//Do what ever you want with values
((SampleBO)entity).setUserName(userName);
}
}
我已将其用于我的BO审计目的,在那里我只是以Json obj的形式获取previousState
和currentState
之间的对象差异.
I have used this for my BO audit purposes in there i just fetch the object difference between previousState
and currentState
as a Json obj.
ObjectDiffVo类
class ObjectDiffVo{
propertyName;
oldValue;
newValue;
//getter and setters
}
CompareObjDiff函数
private List<ObjectDiffVo> compareObjDiff(Object[] currentState,
Object[] previousState, String[] propertyNames) {
List<ObjectDiffVo> odVoL = new ArrayList<>();
for (int i = 0; i < propertyNames.length; i++) {
if (previousState[i] != currentState[i]) {
ObjectDiffVo odVo = new ObjectDiffVo();
odVo.setPropertyName(propertyNames[i]);
odVo.setOldValue(previousState[i]);
odVo.setNewValue(currentState[i]);
odVoL.add(odVo);
}
}
return odVoL;
}
希望这会对您有所帮助.
Hope this will help you.
这篇关于onFlushDirty()的先前状态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!