开发者

Data not getting inserted into audit tables in Spring-Hibernate-envers webapp

开发者 https://www.devze.com 2023-02-14 18:15 出处:网络
I am working on a Spring and Hibernate web app. I have to use Envers for auditing. But when I am running the application only audit tables are created but no data is inserted to the audit tables.

I am working on a Spring and Hibernate web app. I have to use Envers for auditing.

But when I am running the application only audit tables are created but no data is inserted to the audit tables.

Config file:

<mvc:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>    
<bean name="callbackHandler" class="org.hibernate.ejb.event.EntityCallbackHandler" />
<bean name="auditEventListener" class="org.hibernate.envers.event.AuditEventListener" />
<bean name="ejb3PostInsertEventListener" class="org.hibernate.ejb.event.EJB3PostInsertEventListener">
  <property name="callbackHandler"><ref bean="callbackHandler"/></property>
</bean>
<bean name="ejb3PostUpdateEventListener" class="org.hibernate.ejb.event.EJB3PostUpdateEventListener">
  <property name="callbackHandler">
    <ref bean="callbackHandler" />
  </property>
</bean>
<bean name="ejb3PostDeleteEventListener" class="org.hibernate.ejb.event.EJB3PostDeleteEventListener">
  <property name="callbackHandler">
    <ref bean="callbackHandler" />
  </property>
</bean>
<bean id="sessionFactory" name="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
  lazy-init="false">
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
    <list>        
      <value>foo.bar.Person</value>
    </list>
  </property>

  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.max_fetch_depth">2</prop>
      <prop key="hibernate.default_batch_fetch_size">8</prop>
      <prop key="hibernate.jdbc.batch_size">20</prop>
      <prop key="hibernate.jdbc.batch_versioned_data">20</prop>
      <prop key="hibernate.generate_statistics">true</prop>
      <prop key="hibernate.jdbc.fetch_size">100</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
  </property>
  <property name="eventListeners">
    <map>
      <entry key="post-insert">
        <list>
          <ref bean="ejb3PostInsertEventListener" />
          <ref bean="auditEventListener" />
        </list>
      </entry>
      <entry key="post-update">
        <list>
          <ref bean="ejb3PostUpdateEventListener" />
          <ref bean="auditEventListener" />
        </list>
      </entry>
      <entry key="post-delete">
        <list>
          <ref bean="ejb3PostDeleteEventListener" />
          <ref bean="auditEventListener" />
        </list>
      </entry>
      <entry key="pre-collection-update">
        <ref bean="auditEventListener" />
      </entry>
      <entry key="pre-collection-remove">
        <ref bean="auditEventL开发者_如何学Cistener" />
      </entry>
      <entry key="post-collection-recreate">
        <ref bean="auditEventListener" />
      </entry>
    </map>
  </property>   
</bean>

I am using @Transactional annotation in service classes. Thanks in advance.


Figured it out but could not answer it earlier. Better late then never :-).
We have to use <tx:annotation-driven> for this.
Also I think there's some problem with Spring's transaction management and saveOrUpdate of Hibernate template. When used together, saveOrUpdate throws nonUniqueKey exception, but merge works fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消