HOWTO: Envers with Spring
mvankempen Jul 29, 2009 7:46 AMHi,
Since I had some problems getting Envers to work with our Spring project, I though I'd share my notes with you. Additionally I had some "fun" in getting it to work with Oracle. I generated the version schema by hand.
Setup:
- Spring 2.5.6
- Hibernate 3.2.6
- Envers 1.1.0-GA
We are using Hibernate directly without using JPA, so the standard configuration does not apply, here is what you need to do:
in your applicationContext.xml add:
<!-- Event listener for the Envers auditing framework, see sessionFactory configuration --> <bean id="versionsEventListener" class="org.jboss.envers.event.VersionsEventListener" />
in your sessionFactory bean add:
<property name="eventListeners"> <map> <entry key="post-insert" value-ref="versionsEventListener" /> <entry key="post-update" value-ref="versionsEventListener" /> <entry key="post-delete" value-ref="versionsEventListener" /> <entry key="pre-collection-update" value-ref="versionsEventListener" /> <entry key="pre-collection-remove" value-ref="versionsEventListener" /> <entry key="post-collection-recreate" value-ref="versionsEventListener" /> </map> </property>
Since Oracle does not like names to begin with an underscore we need to add two more properties to the hibernateProperties of your sessionFactory, see also
http://www.jboss.org/envers/configuration.html
and
http://www.jboss.org/envers/conf_oracle.html
<property name="hibernateProperties"> <props> <prop key="org.jboss.envers.revisionTypeFieldName">VER_REV_TYPE</prop> <prop key="org.jboss.envers.revisionFieldName">VER_REV</prop> </props> </property>
Finally, we need to rename the default revisions info table, since its name starts with an underscore and Oracle does not like that, this can be achieved by introducing a custom RevisionEntity which extends DefaultRevisionEntity:
/** * Oracle doesn't like the default revisions entity table name (_revisions_info, Oracle doesn't allow names to start * with '_') * */ @Entity @RevisionEntity public class OracleRevisionEntity extends DefaultRevisionEntity { // should be empty }
Debugging note: when Envers can't save data in a version table you won't see an exception in your console, however when you look in your hibernate log file (enable DEBUG) you'll see what is going on.
Cheers,
Marc.