Hibernate环境不工作



我只想使用hibernate环境来审计我的实体。我使用的是envers-1.2.2.ga-hibernate-3.3.jar、hibernate-annotations-3.5.6-Final.jar、hibinate-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>

感谢您的支持

您需要使用兼容版本的Hibernate和Envers。您使用的是Hibernate 3.3、Hibernate核心3.5.2和注释3.5.6的Envers jar。

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

最新更新