使用HibernateTemplate与Spring进行Hibernate Envers [英] Hibernate Envers with Spring using HibernateTemplate
问题描述
我正在尝试在Spring环境中设置Envers.当我从SessionFactory手动检索会话并将所有内容放入Transaction中时,一切工作正常:
I'm trying to setup Envers in a Spring environment. Everything works fine, when I retrieve a session manually from the SessionFactory and put everything inside a Transaction:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
TestEntity test = new TestEntity();
test.setTest("REV1");
session.save(test);
tx.commit();
tx = session.beginTransaction();
test.setTest("REV2");
session.save(test);
tx.commit();
上面的代码将数据插入到TestEntity表中,还更新了AUD表和REVINFO表.
The above code inserts data into the TestEntity-table and also updates the AUD- and REVINFO-tables.
但是,当我使用DAO更新表时,特定于Envers的表没有任何反应.我怀疑原因可能是在DAO中使用HibernateTemplate 使用HibernateTemplate .
However, when I update the table using our DAO, nothing happens to the Envers-specific tables. I suspect the reason might be using HibernateTemplate in the DAO.
HibernateDaoSupport dao;
...
HibernateTemplate hibernateTemplate = dao.getHibernateTemplate();
TestEntity test = new TestEntity();
test.setTest("REV1");
hibernateTemplate.saveOrUpdate(test);
test.setTest("REV2");
hibernateTemplate.saveOrUpdate(test);
我的SessionFactory的Spring配置如下:
My Spring-config for the SessionFactory looks like this:
<bean id="test-sessionFactory" class=...>
<property name="dataSource" ref="test-dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="post-insert" value-ref="enversListener" />
<entry key="post-update" value-ref="enversListener" />
<entry key="post-delete" value-ref="enversListener" />
<entry key="pre-collection-update" value-ref="enversListener" />
<entry key="pre-collection-remove" value-ref="enversListener" />
<entry key="post-collection-recreate" value-ref="enversListener" />
</map>
</property>
</bean>
我已经阅读了很多论坛和博客文章,包括此一个,该链接与Envers常见问题解答中的链接有关,但在我的情况下似乎没有任何提及.
I have read quite a lot of forum- and blog entries including this one, that is linked to from the Envers FAQ, but nothing mentioned there seems to work in my situation.
有人知道,是否有可能使用HibernateTemplate(而不是JPA)将Envers配置为与Spring一起使用?我需要更改配置的哪些部分?在这种情况下,Hibernate文档/API的哪一部分值得研究(恐怕我还不是Hibernate专家)?有人可以提供工作配置的代码示例吗?
Does anyone know, if it's possible to configure Envers to work together with Spring using HibernateTemplate (instead of JPA)? What parts of my configuration do I need to change? What part of the Hibernate documentation/API is worth looking at in this context (I'm afraid I'm not an Hibernate expert yet)? Could someone provide a code sample of a working configuration?
我正在使用Spring 3.0.1和Hibernate 3.5.1.
I'm using Spring 3.0.1 and Hibernate 3.5.1.
推荐答案
问题是由以下事实引起的:Envers依赖正确的事务管理,而HibernateTemplate
在事务之外使用时退回到自动提交"模式,因此Envers在这种情况下不起作用.
The problem is caused by the fact that Envers relies on proper transaction management, whereas HibernateTemplate
falls back to "auto-commit" mode when used outside of transaction, so that Envers doesn't work in this case.
因此,您需要配置事务管理才能使用Envers.在事务内调用HibernateTemplate
的方法时,Envers可以正常工作.
Therefore you need to configure transaction management in order to use Envers. When HibernateTemplate
's methods are called inside a transaction, Envers works fine.
另请参见:
- 10. Transaction Management
- 13.3 Hibernate
这篇关于使用HibernateTemplate与Spring进行Hibernate Envers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!