我添加envers到现有的休眠实体。一切都是那么远就审计工作的顺利进行,但查询是不同的问题,因为修订表不会与现有的数据填充。有没有其他人已经解决了这个问题?也许你已经发现了一些办法来用现有的表修订表?只要想到我会问,我相信其他人会觉得它有用。

有帮助吗?

解决方案

您不需要。结果 AuditQuery可以让你同时获得RevisionEntity和数据修改方式:

AuditQuery query = getAuditReader().createQuery()
                .forRevisionsOfEntity(YourAuditedEntity.class, false, false);

这将构造一个查询返回对象[3]的列表。最前一页元件是你的数据,第二个是修改实体和第三是修改的类型。

其他提示

我们通过运行一系列原始的SQL查询来模拟“插入”现有的所有实体,好像他们刚刚在同一时间创建填充的初始数据。例如:

insert into REVINFO(REV,REVTSTMP) values (1,1322687394907); 
-- this is the initial revision, with an arbitrary timestamp

insert into item_AUD(REV,REVTYPE,id,col1,col1) select 1,0,id,col1,col2 from item; 
-- this copies the relevant row data from the entity table to the audit table

请注意的 REVTYPE 值的 0 以指示刀片(相对于变形例)。

您将在这个类别中有一个问题,如果你正在使用Envers的 ValidityAuditStrategy 和具有已创建比Envers其他数据使能。

在我们的情况下(休眠4.2.8.Final)碱性对象更新抛出(记录为[org.hibernate.AssertionFailure] HHH000099)“不能为实体和更新先前的版本”。

我花了一段时间来找到这个讨论/解释这样交叉发布:

ValidityAuditStrategy没有审核记录

我们已经解决了与现有数据填充审计日志的问题如下:

SessionFactory defaultSessionFactory;

// special configured sessionfactory with envers audit listener + an interceptor 
// which flags all properties as dirty, even if they are not.
SessionFactory replicationSessionFactory;

// Entities must be retrieved with a different session factory, otherwise the 
// auditing tables are not updated. ( this might be because I did something 
// wrong, I don't know, but I know it works if you do it as described above. Feel
// free to improve )

FooDao fooDao = new FooDao();
fooDao.setSessionFactory( defaultSessionFactory );
List<Foo> all = fooDao.findAll();

// cleanup and close connection for fooDao here.
..

// Obtain a session from the replicationSessionFactory here eg.
Session session = replicationSessionFactory.getCurrentSession();

// replicate all data, overwrite data if en entry for that id already exists
// the trick is to let both session factories point to the SAME database.
// By updating the data in the existing db, the audit listener gets triggered,
// and inserts your "initial" data in the audit tables.
for( Foo foo: all ) {
    session.replicate( foo, ReplicationMode.OVERWRITE ); 
}     

的我的数据源的配置(通过Spring):

<bean id="replicationDataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver"/>
  <property name="url" value=".."/>
  <property name="username" value=".."/>
  <property name="password" value=".."/>
  <aop:scoped-proxy proxy-target-class="true"/>
</bean>

<bean id="auditEventListener" 
      class="org.hibernate.envers.event.AuditEventListener"/>

<bean id="replicationSessionFactory"
      class="o.s.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

  <property name="entityInterceptor">
    <bean class="com.foo.DirtyCheckByPassInterceptor"/>
  </property>

  <property name="dataSource" ref="replicationDataSource"/>
  <property name="packagesToScan">
    <list>
      <value>com.foo.**</value>
    </list>
  </property>

  <property name="hibernateProperties">
    <props>
      ..
      <prop key="org.hibernate.envers.audit_table_prefix">AUDIT_</prop>
      <prop key="org.hibernate.envers.audit_table_suffix"></prop>
    </props>
  </property>
  <property name="eventListeners">
    <map>
      <entry key="post-insert" value-ref="auditEventListener"/>
      <entry key="post-update" value-ref="auditEventListener"/>
      <entry key="post-delete" value-ref="auditEventListener"/>
      <entry key="pre-collection-update" value-ref="auditEventListener"/>
      <entry key="pre-collection-remove" value-ref="auditEventListener"/>
      <entry key="post-collection-recreate" value-ref="auditEventListener"/>
    </map>
  </property>
</bean>

在拦截器:

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
..

public class DirtyCheckByPassInterceptor extends EmptyInterceptor {

  public DirtyCheckByPassInterceptor() {
    super();
  }


  /**
   * Flags ALL properties as dirty, even if nothing has changed. 
   */
  @Override
  public int[] findDirty( Object entity,
                      Serializable id,
                      Object[] currentState,
                      Object[] previousState,
                      String[] propertyNames,
                      Type[] types ) {
    int[] result = new int[ propertyNames.length ];
    for ( int i = 0; i < propertyNames.length; i++ ) {
      result[ i ] = i;
    }
    return result;
  }
}

PS:记住,这是一个简化的例子。它不会开箱的,但它会引导你走向一个工作的解决方案。

看一看 http://www.jboss.org /files/envers/docs/index.html#revisionlog

基本上可以使用@RevisionEntity注解定义自己的“修订类型”, 然后实施RevisionListener接口插入你额外的审计数据, 像当前用户和高层次的操作。通常是那些从ThreadLocal的上下文拉动。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top