开始使用envers + hibernate(简单和完整示例)

时间:2009-05-20 14:53:51

标签: java hibernate hibernate-envers

我有Hibernate工作,想尝试添加Envers audit/revision functionality,但似乎无法弄清楚需要什么。 (我的例子编译并运行正常,我得到了常规的Hibernate功能,但我的数据库中没有出现审计表。)有人在那之前做过这个吗?它是否适用于使用HSQLDB方言的H2数据库?网络上是否有一个简单且完整的示例程序?

编辑:让我稍微改写一下。最后,我希望我的构建过程创建一个.jar文件,我可以在另一台计算机上安装,并使用相应的.properties文件和JDBC驱动程序,创建(或允许我创建)相应的数据库表格,如果它们尚未存在。我怎么能这样做?

编辑到目前为止,如果我想运行Jamie B建议的ant任务,我必须调整我的类路径,以便找到envers jar文件和hibernate-tools嵌入在Hibernate工具zip中的jar文件。我还没有把事情搞定。如果/当我这样做,我想也许我可以创建一个SQL文件并将其作为资源放在我的最终.jar文件中,然后我可以在我的程序本身中使用它。 (虽然在我脑海中出现了一面红旗,想着安全问题......嗯....)

3 个答案:

答案 0 :(得分:2)

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

hibernate.hbm2ddl.auto

来自documentation

  

自动验证或导出   架构DDL到数据库的时候   SessionFactory已创建。

这会自动创建根据您设置的envers properties命名的架构表。不需要额外的库或ant任务。

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

答案 1 :(得分:1)

您是否阅读过参考文件的第6章(www.jboss.org/file-access/default/members/envers/downloads/envers-1.2.0.ga-hibernate-3.3.pdf)?看起来_AUD表不是以标准的Hibernate方式创建的;有一个AntTask可以增强它。

答案 2 :(得分:1)

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

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

出现

错误因为表存在,所以我认为,参数hibernate.hbm2ddl.auto = update会解决它

日志:

    5755 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: myappcompany.base.Company_AUD -> company_AUD
5782 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO
5915 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
6746 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
6784 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
6785 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
9004 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table company (ID bigint not null auto_increment, CREATED datetime, CREATED_BY varchar(255), MODIFIED datetime, MODIFIED_BY varchar(255), NAME varchar(255) not null, primary key (ID))
9004 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Table 'company' already exists
10227 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete

我的配置:

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

几天后我会做一个完整的例子