Spring 3.x 和 Hibernate Envers
我在让 Hibernate Envers 在我们的环境中工作时遇到问题。我们使用 Spring 3.x 和 LoadTimeWeaving。下面是我们的上下文文件:
<context:annotation-config/>
<context:spring-configured/>
<context:load-time-weaver aspectj-weaving="autodetect"/>
<context:component-scan base-package="com.viridityenergy.vpower"/>
<context:property-placeholder location="classpath:db/database-test.properties"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="dataSourcePooled"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:jdbcUrl="${database.url}"
p:user="${database.username}"
p:password="${database.password}"
p:initialPoolSize="1"
p:maxPoolSize="5"
p:idleConnectionTestPeriod="500"
p:acquireIncrement="1"
p:maxStatements="50"
p:numHelperThreads="1"
p:autoCommitOnClose="true"/>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:databasePlatform="${database.platform}"
p:showSql="${database.showSql}"
p:generateDdl="${database.generateDdl}"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="TEST"
p:persistenceXmlLocation="META-INF/persistence.xml"
p:dataSource-ref="dataSourcePooled"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
p:dataSource-ref="dataSourcePooled"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
这是我们的 persistence.xml
<persistence-unit name="TEST" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.event.post-insert"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-update"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-delete"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.pre-collection-update"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.pre-collection-remove"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-collection-recreate"
value="org.hibernate.envers.event.AuditEventListener"/>
</properties>
</persistence-unit>
审计表设置正确,但是当实体被持久化时,审计表中没有存储任何记录。只有两个字段需要审核。另外,因为我们使用的是 loadTimeWeaving,所以当我们运行单元测试时,我们需要 -javaagent:/Users/TEST/.m2/repository/org/springframework/spring-instrument/3.0.3.RELEASE/spring-instrument- 3.0.3.RELEASE.jar 作为 JVM 参数,以便 loadTimeWeaving 工作。
除了恩弗斯,其他一切都很好。
任何帮助将不胜感激。
I'm having a problem getting Hibernate Envers to work in our environment. We are using Spring 3.x with LoadTimeWeaving. Below is our context file:
<context:annotation-config/>
<context:spring-configured/>
<context:load-time-weaver aspectj-weaving="autodetect"/>
<context:component-scan base-package="com.viridityenergy.vpower"/>
<context:property-placeholder location="classpath:db/database-test.properties"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="dataSourcePooled"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:jdbcUrl="${database.url}"
p:user="${database.username}"
p:password="${database.password}"
p:initialPoolSize="1"
p:maxPoolSize="5"
p:idleConnectionTestPeriod="500"
p:acquireIncrement="1"
p:maxStatements="50"
p:numHelperThreads="1"
p:autoCommitOnClose="true"/>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:databasePlatform="${database.platform}"
p:showSql="${database.showSql}"
p:generateDdl="${database.generateDdl}"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="TEST"
p:persistenceXmlLocation="META-INF/persistence.xml"
p:dataSource-ref="dataSourcePooled"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
p:dataSource-ref="dataSourcePooled"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
Here is our persistence.xml
<persistence-unit name="TEST" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.event.post-insert"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-update"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-delete"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.pre-collection-update"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.pre-collection-remove"
value="org.hibernate.envers.event.AuditEventListener"/>
<property name="hibernate.ejb.event.post-collection-recreate"
value="org.hibernate.envers.event.AuditEventListener"/>
</properties>
</persistence-unit>
The audit tables are setup correctly, but when an entity is persisted there are no records stored in the audit tables. There are only two fields that are audited. Also, because we are using loadTimeWeaving, when we run unit test, we are required to have -javaagent:/Users/TEST/.m2/repository/org/springframework/spring-instrument/3.0.3.RELEASE/spring-instrument-3.0.3.RELEASE.jar as a JVM argument so the loadTimeWeaving works.
Aside from Envers, everything else works fine.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题所在,所以对于其他可能遇到此问题的人来说,这就是问题所在。恩弗斯实际上一直在工作。我没有意识到的是,在我的单元测试中,所有内容都包含在单个事务中,然后回滚。在初始事务提交并完成之前,Envers 不会提交审计表。
解决方法是将测试设置为集成测试而不进行回滚。然后我的审计记录开始出现。
I found out what the problem was, so for anyone else who may be experiencing this, here it is. Envers was actually working all along. What I didn't realize was that in my unit tests, everything was wrapped in a single transaction and then rolled back. Envers will not commit to the audit table until the initial transaction commits and is complete.
The fix was to set the test up as an integration test without a rollback. Then my audit records started showing up.