2014-11-04 2 views

답변

0

:

또한 여기에 유사한 기능의 또 다른 예이다. Interceptor 인터페이스를 사용하는 경우 아래에서 재정의 된 방법을 사용하여 별도의 테이블에 데이터를 저장하십시오. 당신은 EmptyInterceptor에게 모든 메소드를 오버라이드 (override) 할 필요를 확장하지 않으면

public boolean onLoad(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public boolean onFlushDirty(Object o, Serializable srlzbl, Object[] os, Object[] os1, String[] strings, Type[] types) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public boolean onSave(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void onDelete(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void onCollectionRecreate(Object o, Serializable srlzbl) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void onCollectionRemove(Object o, Serializable srlzbl) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void onCollectionUpdate(Object o, Serializable srlzbl) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void preFlush(Iterator itrtr) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void postFlush(Iterator itrtr) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public Boolean isTransient(Object o) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public int[] findDirty(Object o, Serializable srlzbl, Object[] os, Object[] os1, String[] strings, Type[] types) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public Object instantiate(String string, EntityMode em, Serializable srlzbl) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public String getEntityName(Object o) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public Object getEntity(String string, Serializable srlzbl) throws CallbackException { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void afterTransactionBegin(Transaction t) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void beforeTransactionCompletion(Transaction t) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

public void afterTransactionCompletion(Transaction t) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 


public String onPrepareStatement(String string) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

, 필요한 방법은 DAO 클래스에서 재정의 할 수 있습니다.

예를 들어 내 감사 테이블에 fieldName, fieldValue, fieldType, className, 방법 save() 위해를 저장해야합니다. 업데이트 사용 findDirty() 방법에 대한 삭제 사용 onDelete() 방법

//Audit save Pojo 
public class AuditSave{ 
    private String className; 
    private String fieldName 
    private String fieldValue 
    private String fieldType 
    //setter's and getter's 
} 
//AuditDAO class 
public class AuditDao exteds EmptyInterceptor{ 

    public boolean onSave(Object o, Serializable srlzbl, Object[] os, String[] strings, 
    Type[] types) throws CallbackException { 
    Session session = HibernateUtil.getSessionFactory().openSesson(); 
    String className = o.getClass().getName(); 
    try{ 
     Transaction tx = session.beginTransaction(); 
     for(int i = 0;i < os.length ;i++){ 
      AuditSave auditSave = new AuditSave(); 
      auditSave.setClassName(className); 
      auditSave.setFieldName((String)strings[i]); 
      auditSave.setFieldValue((String)os[i]); 
      auditSave.setFieldType(types[i].toString()); 

      session.save(auditSave); 
      tx.commit(); 
     }catch(Exception e){ 
      tx.rollback(); 
      e.printStackTra; 
     } 
    if(session.isOpen()) 
      session.close(); 
    } 
     return true; 
    } 
// same as update,delete 

,

관련 문제