简体   繁体   English

如何在Struts2中实现休眠拦截器

[英]How to imlement hibernate interceptor in Struts2

在我的Struts2应用程序中,我想使用Hibernate拦截器审核所有CURD操作,请帮助我如何实现。

You can create an interceptor by extending the EmptyInterceptor , read the documentation: 您可以通过扩展EmptyInterceptor来创建拦截器,请阅读文档:

Chapter 12. Interceptors and events 第十二章拦截器和事件

The documentation has an example on how to implement auditing using interceptors. 该文档提供了有关如何使用拦截器实施审核的示例。

Also here is another example for similar functionality : Hibernate interceptor example – audit log 另外,这里还有另一个类似功能的示例Hibernate拦截器示例–审核日志

You can create XXXDAO class by implementing Interceptor or by extending EmptyInterceptor . 您可以通过实现Interceptor或扩展EmptyInterceptor来创建XXXDAO类。 If You use Interceptor interface , using below overridden method save the data in separate table. 如果使用Interceptor接口,则使用下面的覆盖方法将数据保存在单独的表中。

 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.");
}

If You extends EmptyInterceptor no need to override all the methods, required methods can override in your dao class. 如果扩展EmptyInterceptor不需要覆盖所有方法,则必需的方法可以在您的dao类中覆盖。

For example I have to store fieldName , fieldValue , fieldType , className in my Audit table, for save() method. 例如,我必须在我的Audit表中为save()方法存储fieldNamefieldValuefieldTypeclassName

//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

For delete use onDelete() method, For update use findDirty() method 对于删除,请使用onDelete()方法;对于更新,请使用findDirty()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM