Hibernate envers is not working

I just want to use hibernate envers in order to audit my entities.I am using envers-1.2.2.ga-hibernate-3.3.jar,hibernate-annotations-3.5.6-Final.jar,hibernate-core-3.5.2-Final.jar and hibernate-jpa-2.0-api-1.0.0.Final.jar.

My entity is below

User.java

@Entity
@Audited

@Table(name = "ACCT_USER")

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends IdEntity {

    private String loginName;
    private String password;
    private String name;
    private String email;
    private List<Role> roleList = Lists.newArrayList();


    @Column(nullable = false, unique = true)
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }



}

This is my application context where i have configured envers

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-lazy-init="true">

    <description>Spring</description>


    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>

                <value>classpath*:/application.properties</value>
            </list>
        </property>
    </bean>


    <context:component-scan base-package="net.top" />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- Connection Pooling Info -->
        <property name="maxIdle" value="${dbcp.maxIdle}" />
        <property name="maxActive" value="${dbcp.maxActive}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
            </props>
        </property>
        <property name="packagesToScan" value="net.top.*.entity.*" />

    </bean>

<!-- Envers Info -->
     **<bean id="envers" class="org.hibernate.envers.event.AuditEventListener">


       <property name="eventListeners">
            <map>
                <entry key="post-insert" value-ref="envers"/>
                <entry key="post-update" value-ref="envers"/>
                <entry key="post-delete" value-ref="envers"/>
                    <entry key="post-collection-recreate" value-ref="envers"/>
                <entry key="pre-collection-remove" value-ref="envers"/>
                <entry key="pre-collection-update" value-ref="envers"/>
            </map>
          </property>

          </bean>**


    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

There is no error when am executing the application but there is also no audited tables creating when am adding or removing a user.Is there any other configuration i need to do?.

Update

I got it i just edit my application context like this

applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
        default-lazy-init="true">

        <description>Spring</description>


        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            <property name="ignoreResourceNotFound" value="true" />
            <property name="locations">
                <list>

                    <value>classpath*:/application.properties</value>
                </list>
            </property>
        </bean>


        <context:component-scan base-package="net.top" />


        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- Connection Info -->
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />

            <!-- Connection Pooling Info -->
            <property name="maxIdle" value="${dbcp.maxIdle}" />
            <property name="maxActive" value="${dbcp.maxActive}" />
            <property name="defaultAutoCommit" value="false" />
            <property name="timeBetweenEvictionRunsMillis" value="3600000" />
            <property name="minEvictableIdleTimeMillis" value="3600000" />
        </bean>



        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="namingStrategy">
                <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
                </props>
            </property>
<property name="eventListeners">
                <map>
                    <entry key="post-insert" value-ref="envers"/>
                    <entry key="post-update" value-ref="envers"/>
                    <entry key="post-delete" value-ref="envers"/>
                        <entry key="post-collection-recreate" value-ref="envers"/>
                    <entry key="pre-collection-remove" value-ref="envers"/>
                    <entry key="pre-collection-update" value-ref="envers"/>
                </map>
              </property>
            <property name="packagesToScan" value="net.top.*.entity.*" />

        </bean>

    <!-- Envers Info -->
        <bean id="envers" class="org.hibernate.envers.event.AuditEventListener"/>


        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>


        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    </beans>

thanks for your support


You need to use compatible versions of Hibernate and Envers. You are using an Envers jar for Hibernate 3.3, Hibernate-core 3.5.2 and annotations 3.5.6.

Just use the same version everywhere.

链接地址: http://www.djcxy.com/p/71552.html

上一篇: 如何只存储更新的值

下一篇: Hibernate envers不起作用