Java 开始使用envers+;hibernate(简单而完整的示例)

Java 开始使用envers+;hibernate(简单而完整的示例),java,hibernate,hibernate-envers,Java,Hibernate,Hibernate Envers,我正在使用Hibernate,希望尝试添加,但似乎无法确定需要什么。(我的示例编译并运行正常,我获得了常规的Hibernate功能,但我的数据库中没有审计表。)以前有人这样做过吗?它是否使用HSQLDB方言处理H2数据库?网络上是否有一个简单而完整的示例程序 编辑:让我稍微重新措辞一下。最后,我希望我的构建过程创建一个.jar文件,我可以将它安装在另一台计算机上,并使用适当的.properties文件和JDBC驱动程序,创建(或允许我创建)适当的数据库表(如果它们还不存在)。我该怎么做 编辑:到

我正在使用Hibernate,希望尝试添加,但似乎无法确定需要什么。(我的示例编译并运行正常,我获得了常规的Hibernate功能,但我的数据库中没有审计表。)以前有人这样做过吗?它是否使用HSQLDB方言处理H2数据库?网络上是否有一个简单而完整的示例程序

编辑:让我稍微重新措辞一下。最后,我希望我的构建过程创建一个.jar文件,我可以将它安装在另一台计算机上,并使用适当的.properties文件和JDBC驱动程序,创建(或允许我创建)适当的数据库表(如果它们还不存在)。我该怎么做


编辑:到目前为止,如果我想运行Jamie B建议的ant任务,我必须调整我的类路径,让它找到envers jar文件和隐藏在Hibernate tools zip中的jar文件。但我还是没能让事情进展顺利。如果我这样做了,我想也许我可以创建一个SQL文件,并将其作为资源放在最终的.jar文件中,然后我可以从程序本身中使用它。(尽管我脑子里的红旗在思考安全问题……嗯……)

你读过参考文件(www.jboss.org/file access/default/members/envers/downloads/envers-1.2.0.ga-hibernate-3.3.pdf)的第6章了吗?看起来_AUD表不是以标准的Hibernate方式创建的;有一个AntTask对其进行了扩充。

听起来您正在寻找以下hibernate属性:

hibernate.hbm2ddl.auto
从:

自动验证或导出 当 SessionFactory已创建

这将自动创建根据您设置的名称命名的模式表。不需要额外的库或ant任务


一、 例如,将其添加到我的
hibernate.cfg.xml中,设置为
update
,用于我的开发数据库。您还可以使用Hibernate的
配置
对象以编程方式添加此属性

经过一些实验,我用envers生成了工作表

我使用了这些参数 hibernate.hbm2ddl.auto=create drop

出现错误是因为表存在,所以我认为,参数hibernate.hbm2ddl.auto=update将解决此问题

日志:

我的配置:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <!--
    Data Source config 
     -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entity-manager-factory-ref="entityManagerFactory" />

    <!-- 
    JPA config   
    -->
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:persistence-xml-location="${persistence.xml.location}"
        p:data-source-ref="dataSource">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:showSql="true" p:generateDdl="true">
            </bean>
        </property>
        <property name="jpaProperties">
            <value>
                hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
                hibernate.dialect=${hibernate.dialect}
                hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
            </value>
        </property>
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>

ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
hibernate.dialogue=${hibernate.dialogue}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}

过几天我会举一个完整的例子

谢谢你的帖子——这个问题在我的任务队列中排得太远了,我不确定我是否有时间在短时间内查看它,但我真的很感激——我不得不放弃envers方法,因为我只是没有时间去弄清楚如何让它工作。那么,也许你可以使用另一种方法?如果是的话,你能分享一下吗?我想这会有帮助的
<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <!--
    Data Source config 
     -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entity-manager-factory-ref="entityManagerFactory" />

    <!-- 
    JPA config   
    -->
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:persistence-xml-location="${persistence.xml.location}"
        p:data-source-ref="dataSource">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:showSql="true" p:generateDdl="true">
            </bean>
        </property>
        <property name="jpaProperties">
            <value>
                hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
                hibernate.dialect=${hibernate.dialect}
                hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
            </value>
        </property>
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>