2012-10-03 133 views
1

我正在使用hibernate事件偵聽器實現一個簡單的審計日誌功能。我想積累交易中所有實體的所有更改並處理審計。Hibernate事件偵聽器 - postFlush等效

通過使用Hibernate攔截器的方法,我們有postFlush(),我們處理所有的累積審計事件的審計。

什麼是事件監聽器的等價物?

我嘗試使用 'hibernate.ejb.event.flush' 事件。但它甚至在onPostInsert,onPostUpdate和onPostDelete事件之前調用生命週期的開始。所以不能積累變化。

也嘗試自動刷新,它也沒有工作。任何想法?

回答

1

就不要誤導別人標註實體一樣簡單,我理解了它的洗淨事件偵聽器的實際工作。問題是默認的hibernate事件監聽器沒有註冊。當我將默認事件偵聽器與自定義偵聽器一起註冊時,它開始正常工作。

+0

是的!爲我工作... –

+0

不幸的是,在我的情況下,flushEventListener在postUpdate,postInsert和postDelete監聽器之前被調用,所以它不是在Hibernate攔截器上的postFlush equivevelent。 –

0

如果要實現與Hibernate的審計,我會建議你使用Envers。審計與Envers是與@Audited註釋

+0

是啊,我看着envers。我的簡單要求有點沉重。此外,我不想爲每個實體,版本等的表。謝謝 –

0

我審覈通過這種方式,但日期是醜..

的persistence.xml

<property name="hibernate.ejb.interceptor" value="siapen.jpa.interceptor.MeuInterceptador" /> 

Java代碼

import java.io.Serializable; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Iterator; 

import org.apache.commons.lang3.ObjectUtils; 
import org.hibernate.CallbackException; 
import org.hibernate.EmptyInterceptor; 
import org.hibernate.type.Type; 

import siapen.model.BaseEntity; 

public class MeuInterceptador extends EmptyInterceptor { 

    private static final long serialVersionUID = 7853236444153436270L; 

    private String strSQL = ""; 
    String acao; 
    @SuppressWarnings("rawtypes") 
    BaseEntity entity; 
    String s = ""; 

    @SuppressWarnings("unchecked") 
    // 1 
    public boolean onSave(Object obj, Serializable id, Object[] valores, String[] propertyNames, Type[] types) 
      throws CallbackException { 
     if (obj instanceof BaseEntity) { 
      entity = (BaseEntity) obj; 
      for (int i = 0; i < valores.length; i++) { 
       if (valores[i] != null && !valores[i].equals("")) { 
        s += propertyNames[i] + ":" + valores[i]; 
        if (i != valores.length - 1) { 
         s += "___"; 
        } 
       } 
      } 
     } 
     return false; 
    } 

    @SuppressWarnings("unchecked") 
    // 1 
    public boolean onFlushDirty(Object obj, Serializable id, Object[] valoresAtuais, Object[] valoresAnteriores, 
      String[] propertyNames, Type[] types) throws CallbackException { 
     if (obj instanceof BaseEntity) { 
      entity = (BaseEntity) obj; 

      for (int i = 0; i < valoresAtuais.length; i++) { 

       if (!ObjectUtils.equals(valoresAtuais[i], valoresAnteriores[i])) { 
        if (!s.equals("")) { 
         s += "___"; 
        } 
        s += propertyNames[i] + "-Anterior:" + valoresAnteriores[i] + ">>>Novo:" + valoresAtuais[i]; 
       } 
      } 
     } 
     return false; 

    } 

    @SuppressWarnings("unchecked") 
    // 1 
    public void onDelete(Object obj, Serializable id, Object[] state, String[] propertyNames, Type[] types) { 
     if (obj instanceof BaseEntity) { 
      entity = (BaseEntity) obj; 
     } 
    } 

    // CHAMADO ANTES DO COMMIT 
    // 2 
    @SuppressWarnings("rawtypes") 
    public void preFlush(Iterator iterator) { 
    } 

    // 3 
    public String onPrepareStatement(String sql) { 
     acao = ""; 
     if (sql.startsWith("/* update")) { 
      acao = "update"; 
     } else if (sql.startsWith("/* insert")) { 
      acao = "insert"; 
     } else if (sql.startsWith("/* delete")) { 
      acao = "delete"; 
     } 
     if (acao != null) { 
      strSQL = sql; 
     } 
     return sql; 
    } 

    // CHAMADO APÓS O COMMIT 
    // 4 
    @SuppressWarnings("rawtypes") 
    public void postFlush(Iterator iterator) { 
     if (acao != null) { 
      try { 
       if (acao.equals("insert")) { 
        AuditLogUtil audi = new AuditLogUtil(); 
        audi.LogIt("Salvo", entity, s); 
       } 
       if (acao.equals("update")) { 
        AuditLogUtil audi = new AuditLogUtil(); 
        audi.LogIt("Atualizado", entity, s); 
       } 
       if (acao.equals("delete")) { 
        AuditLogUtil audi = new AuditLogUtil(); 
        audi.LogIt("Deletado", entity, ""); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       strSQL = ""; 
       s = ""; 
      } 
     } 
    } 

}