2017-08-17 38 views
0

我一直在試圖調試這個問題。我的目標是試圖從休眠攔截器保存審計。我使用的彈簧啓動(1.5.3)和休眠5.0.12無法插入Hibernate攔截器的審計

下面是從SQL

休眠休眠SQL日誌:選擇NEXTVAL( 'user_sequence')

休眠:插入到用戶( (?,?,?,?,?,?,?,?,?,?,?)的創建時間,刪除,描述,修改,

休眠:選擇NEXTVAL( 'audit_sequence')

POST FLUSH DO NE

正如您所看到的,它獲取審計對象的nextval,但不插入值。

public void postFlush(@SuppressWarnings("rawtypes") Iterator iterator) throws CallbackException { 
    try { 
     AuditRepository auditRepo =(AuditRepository)ApplicationContextProvider.getApplicationContext().getBean("auditRepository"); 
     synchronized(audits) { 
      for (Long id:audits.keySet()) { 
       auditRepo.save(audits.get(id)); 
      } 
     } 



    } catch (Exception e) { 
     logger.error(e.toString(),e.toString()); 
    } finally { 
     synchronized (audits) { 
      audits.clear(); 
     } 
    } 
    System.out.println("POST FLUSH DONE"); 
} 

回答

0

我希望這個幫你,這是我怎麼做我的應用程序(休眠5.2.6):

import java.io.Serializable; 
import java.util.Date; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.hibernate.EmptyInterceptor; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.type.Type; 

import com.demo.domain.AuditLog; 
import com.demo.util.GlobUtil; 

public class CustomInterceptor extends EmptyInterceptor 
{ 
    private static final Logger log = LogManager.getLogger(CustomInterceptor.class); 

    public static final String OPERATION_TYPE_INSERT = "INSERT"; 

    public static final String OPERATION_TYPE_UPDATE = "UPDATE"; 

    public static final String OPERATION_TYPE_DELETE = "DELETE"; 

    public static final String OPERATION_TYPE_SELECT = "SELECT"; 

    // delete 
    public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) 
    { 
     auditTrail(entity, id, null, state, propertyNames, types, OPERATION_TYPE_DELETE); 
    } 

    // update 
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, 
      String[] propertyNames, Type[] types) 
    { 
     return auditTrail(entity, id, currentState, previousState, propertyNames, types, OPERATION_TYPE_UPDATE); 
    } 

    // select 
    public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) 
    { 
     // return auditTrail(entity, id, null, state, propertyNames, types, OPERATION_TYPE_SELECT); 
     return true; 
    } 

    // insert 
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) 
    { 
     return auditTrail(entity, id, state, null, propertyNames, types, OPERATION_TYPE_INSERT); 
    } 

    private boolean auditable(Object entity) 
    { 
     return (entity instanceof Auditable); 
    } 

    private boolean auditTrail(Object entity, Serializable id, Object[] currentState, Object[] previousState, 
      String[] propertyNames, Type[] types, String operationType) 
    { 
     String prev = ""; 
     String curr = ""; 

     Session session = HibernateUtil.getSessionFactory().openSession(); 

     if (!auditable(entity)) 
     { 
      return false; 
     } 

     try 
     { 
      for (int index = 0; index < propertyNames.length; index++) 
      { 
       if (previousState != null) 
       { 
        prev = (previousState[index] == null) ? "" : previousState[index].toString(); 
       } 

       if (currentState != null) 
       { 
        curr = (currentState[index] == null) ? "" : currentState[index].toString(); 
       } 

       AuditLog auditLog = new AuditLog(id.toString(), entity.getClass().toString(), 
         propertyNames[index], prev, curr, operationType, GlobUtil.getUserName(), new Date()); 

       session.beginTransaction(); 
       session.save(auditLog); 
       session.getTransaction().commit(); 
      } 
     } 
     catch (HibernateException e) 
     { 
      session.getTransaction().rollback(); 
      log.error("Unable to process audit log for " + operationType + " operation", e); 
     } 
     finally 
     { 
      session.close(); 
     } 

     return true; 
    } 
} 

,當然還有XML或Java中聲明CustomInterceptor

<property name="entityInterceptor"> 
    <bean class="com.demo.common.CustomInterceptor"/> 
</property> 
+0

我使用的春天開機。在application.properties中,spring.jpa.properties.hibernate.ejb.interceptor = AuditInterceptor。我正在使用spring jpa和sping數據。我會嘗試使用HibernateUtil的方法。但我想得到它與春季工作 – Paul

+0

@paul我實際上在春天做它,但不是春季啓動。 – shadow

+0

@paul在這一行做一個斷點service.add(audits.get(id));看看裏面是否有價值? – shadow

0

我解決了這個我的自我。我改變了代碼爲EntityManager.persist() doesn't insert data when using @Transactional

@Override 
public void postFlush(@SuppressWarnings("rawtypes") Iterator iterator) throws CallbackException { 
    try { 
     AuditService service = (AuditService)ApplicationContextProvider.getApplicationContext().getBean("audit"); 

     synchronized(audits) { 
      for (Long id:audits.keySet()) { 
       service.add(audits.get(id)); 
      } 
     } 
    } catch (Exception e) { 
     logger.error(e.toString(),e.toString()); 
    } finally { 
     synchronized (audits) { 
      audits.clear(); 
     } 
    } 
} 


@Service("audit") 
public class AuditServiceImpl implements AuditService { 

@PersistenceContext(type = PersistenceContextType.EXTENDED) 
private EntityManager em; 


public Audit add(Audit obj) { 
    em.persist(obj); 

    return obj; 

} 

}建議