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

发布于 2024-07-21 07:01:25 字数 812 浏览 8 评论 0原文

我有 Hibernate 正在工作,并且想尝试添加 Envers 审核/修订功能,但似乎无法弄清楚需要什么。 (我的示例编译并运行正常,我获得了常规的 Hibernate 功能,但我的数据库中没有出现审核表。)以前有人这样做过吗? 它可以使用 HSQLDB 方言与 H2 数据库一起使用吗? 网络上有简单且完整的示例程序吗?

编辑:让我稍微改一下。 最后,我希望我的构建过程创建一个 .jar 文件,我可以将其安装在不同的计算机上,并使用适当的 .properties 文件和 JDBC 驱动程序,将创建(或允许我创建)适当的数据库表(如果它们尚不存在)。 我怎样才能做到这一点?

编辑:到目前为止,如果我想运行 Jamie B 建议的 ant 任务,我必须调整我的类路径,以便它找到 envers jar 文件和 hibernate-tools jar 文件,隐藏在 Hibernate 工具 zip 中。 而且我还没有让事情发挥作用。 如果/当我这样做时,我想也许我可以创建一个 SQL 文件并将其作为资源放入我的最终 .jar 文件中,然后我可以在我的程序本身中使用它。 (尽管考虑到安全问题,我的脑海中响起了危险信号……嗯……)

I have Hibernate working and would like to try adding Envers audit/revision functionality but can't seem to figure out what is required. (my example compiles and runs OK and I get the regular Hibernate functionality but no audit tables appear in my database.) Has someone out there done this before? Does it work with H2 database using HSQLDB dialect? Is there a simple and complete example program on the web?

edit: let me rephrase slightly. In the end, I'd like my build process to create a .jar file, that I can install on a different computer, and with the appropriate .properties file and JDBC driver, will create (or allow me to create) the appropriate database tables if they are not already present. How can I do this?

edit: well so far, if I want to run the ant task that Jamie B has suggested, I have to tweak my classpath so it finds the envers jar file and the hibernate-tools jar file that is buried inside the Hibernate tools zip. And I still haven't gotten things working. If/when I do, I think maybe then I can create an SQL file and put it as a resource within my final .jar file, that then I can use from within my program itself. (although a red flag goes off in my head thinking about security issues... hmm....)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2024-07-28 07:01:25

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

hibernate.hbm2ddl.auto

来自 文档

自动验证或导出
schema DDL 到数据库时
SessionFactory 已创建。

这会自动创建根据 envers 属性 命名的模式表你已经设置了。 不需要额外的库或 Ant 任务。

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

It sounds like you are looking for the following hibernate property:

hibernate.hbm2ddl.auto

From the documentation:

Automatically validate or export
schema DDL to the database when the
SessionFactory is created.

This automatically creates the schema tables named according to the envers properties you've set. No additional libraries or ant tasks needed.

I, for example, add this to my hibernate.cfg.xml set to update for my development databases. You can also add this property programatically using Hibernate's configuration object.

蓝戈者 2024-07-28 07:01:25

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

Did you read Chapter 6 of the Reference Document (www.jboss.org/file-access/default/members/envers/downloads/envers-1.2.0.ga-hibernate-3.3.pdf)? Looks like the _AUD tables do not get created in the standard Hibernate way; there's an AntTask that augments it.

回眸一笑 2024-07-28 07:01:25

经过一些实验后,我使用 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>

几天后我将制作一个完整的示例

after some experiments I've got working table generation with envers

I used these parameter
hibernate.hbm2ddl.auto=create-drop

error appears because table exists, so I think, that parameter hibernate.hbm2ddl.auto=update will solve it

Log:

    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

my config:

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

At few days I'll make a full example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文