2016-09-15 51 views
-1

如何在EmptyInterceptor的onFlushDirty()方法中獲取實體的前一個狀態對象?我可以擁有當前會話和當前實體對象,但獲取該實體對象的先前狀態不會出現。請幫助onFlushDirty的先前狀態對象

回答

0

您可以使用Object[] previousState and String[] propertyNames參數訪問先前的對象值。

@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 previousStatecurrentState的對象不同。

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

希望這會幫助你。