Hibernate envers无法正常工作

时间:2012-08-03 07:43:16

标签: hibernate hibernate-envers

我只是想使用hibernate envers来审核我的实体。我使用的是envers-1.2.2.ga-hibernate-3.3.jar,hibernate-annotations-3.5.6-Final.jar,hibernate-core- 3.5.2-Final.jar和hibernate-jpa-2.0-api-1.0.0.Final.jar。

我的实体位于

之下

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;
    }



}

这是我已配置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>

执行应用程序时没有错误,但是在添加或删除用户时也没有创建审计表。是否还有其他配置需要做什么?。

更新

我知道了我只是像这样编辑我的应用程序上下文

的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>

感谢您的支持

1 个答案:

答案 0 :(得分:1)

您需要使用兼容版本的Hibernate和Envers。您正在为Hibernate 3.3,Hibernate-core 3.5.2和注释3.5.6使用Envers jar。

在任何地方使用相同的版本。