【问题标题】:How to generate Envers database schema with org.hibernate.tool.EnversSchemaGenerator?如何使用 org.hibernate.tool.EnversSchemaGenerator 生成 Envers 数据库模式?
【发布时间】:2012-03-27 16:46:41
【问题描述】:

我将 Hibernate 更新到了 4.1.1.Final 版本。根据the documentation 有两种方法可以生成数据库模式:

  1. 蚂蚁任务org.hibernate.tool.ant.EnversHibernateToolTask
  2. 从 Java 运行 org.hibernate.tool.EnversSchemaGenerator

Hibernate-tools 不适用于 Hibernate-4.1.1.Final。它有一个blocking bug

我只找到release notestest case。 那么如何在我的 persistence.xml 和 Maven 中使用org.hibernate.tool.EnversSchemaGenerator

更新:

找到相关的thread on the Hibernate forum。我的问题似乎还没有答案。

【问题讨论】:

    标签: hibernate hibernate-envers


    【解决方案1】:

    Juplo 已创建 Maven plugin for Hibernate 4。该插件支持模式导出,包括 Envers。工作示例如下。检查official plugin configuration documentation 以获取使用选项的说明。

    插件在test 目标的Maven /target 目录中生成schema.sql 文件。 或者您可以手动运行hibernate4:export 目标来更新文件。

    <project>
        <build>
            <plugins>
                <plugin>
                    <groupId>de.juplo</groupId>
                    <artifactId>hibernate4-maven-plugin</artifactId>
                    <version>1.0.3</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>export</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <envers>true</envers>
                        <format>true</format>
                        <delimiter>;</delimiter>
                        <force>true</force>
                        <type>CREATE</type>
                        <target>SCRIPT</target>
                        <hibernateDialect>org.hibernate.dialect.PostgreSQL9Dialect</hibernateDialect>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    【讨论】:

    • 非常感谢,这很容易。
    【解决方案2】:

    您不需要 Ant 或 Hibernate 工具。直接使用 EnversSchemaGenerator 非常简单,如下所示:

    Configuration config = new Configuration();
    
    //make sure you set the dialect correctly for your database (oracle for example below)
    config.setProperty("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect");
    
    //add all of your entities
    config.addAnnotatedClass(MyAnnotatedEntity.class);
    
    SchemaExport export = new EnversSchemaGeneHator(config).export();
    export.execute(true, false, false, false);
    

    你也可以给它一个要写入的文件名,但是上面的代码无论如何都会打印到系统日志中。

    【讨论】:

    • 是错字吗? hibernate 文档中没有 EnversSchemaGeneHator.class 但有 EnversSchemaGenerator.class
    【解决方案3】:

    以下内容对我有用:

    public static void main(String[] args) {
        Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure("persistenceUnit", null);
        jpaConfiguration.buildMappings();
        Configuration hibernateConfiguration = jpaConfiguration.getHibernateConfiguration();
        AuditConfiguration.getFor(hibernateConfiguration);
        EnversSchemaGenerator esg = new EnversSchemaGenerator(hibernateConfiguration);
        org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export();
        se.setOutputFile("sql/schema.sql");
        se.setFormat(true);
        se.setDelimiter(";");
        se.drop(true, false);
        se.create(true, false);
    }
    

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题。现在有一个 Hibernate 4 Tools 版本:

          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-tools</artifactId>
              <version>4.0.0-CR1</version>
          </dependency>
      

      但是这个 Ant 片段不导出审计表,只导出“基本”表:

      <target name="schema-export">
          <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.EnversHibernateToolTask" classpathref="classpath"/>
          <hibernatetool destdir="sql">
              <classpath refid="classpath"/>
              <jpaconfiguration persistenceunit="persistenceUnit"/>
              <hbm2ddl export="false" create="true" drop="true" format="true" outputfilename="schema.sql"/>
          </hibernatetool>
      </target>
      

      与此代码相同:只有“基本”表,没有“_aud”表:

      public static void main(String[] args) {
          Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure("persistenceUnit", null);
          Configuration hibernateConfiguration = jpaConfiguration.getHibernateConfiguration();
          AuditConfiguration.getFor(hibernateConfiguration);
          EnversSchemaGenerator esg = new EnversSchemaGenerator(hibernateConfiguration);
          org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export();
          se.setOutputFile("sql/schema.sql");
          se.setFormat(true);
          se.setDelimiter(";");
          se.drop(true, false);
          se.create(true, false);
      }
      

      你还有兴趣吗?如果我找到解决问题的方法,我会告诉你的。也许其他人对我们有什么建议?

      【讨论】:

      • 检查我对这个问题的回答。我找到了完成任务的最便捷方式。
      猜你喜欢
      • 2011-09-20
      • 2010-09-26
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多