org.hibernate.tool.enversSchemageneratorを使用して監督データベーススキーマを生成する方法?

StackOverflow https://stackoverflow.com//questions/9647528

  •  10-12-2019
  •  | 
  •  

質問

Hibernateを4.1.1.finalバージョンに更新しました。 ドキュメント データベーススキーマを生成する方法は2つあります。

  1. Antタスクorg.hibernate.tool.ant.EnversHibernateToolTask
  2. Javaからorg.hibernate.tool.EnversSchemaGeneratorを実行します。

    Hibernate-ToolsはHibernate-4.1.1.finalでは機能しません。 ブロッキングバグ

    私はリリースノートテストケース。 それで、私はmy persistence.xmlとmavenを使ってorg.hibernate.tool.EnversSchemaGeneratorを使うことができますか?

    更新:

    href="htps:/forum.hibernate.org/viewtopic.php?f=1&t= 1014593" REL="NOFOLLOW"> Hibernate Forum のスレッド。私の質問にまだ答えがないようです。

役に立ちましたか?

解決

Juplo has created Maven plugin for Hibernate 4. The plugin supports schema export including Envers. The working example is below. Check official plugin configuration documentation to get explanation for used options.

The plugin generates schema.sql file in the Maven /target directory on test goal. Or you can manually run hibernate4:export goal to update the file.

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

他のヒント

You don't need Ant or Hibernate tools. It's pretty easy to just use the EnversSchemaGenerator directly, like this:

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);

You can also give it a file name to write to, but the code above will print to the syslog anyway.

The following worked for me:

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);
}

I have got the same problem. Now there is a Hibernate 4 Tools version:

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

But this Ant fragment does not export the audit tables, only the "basic" tables:

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

Same with this code: Only "basic", no "_aud" tables:

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);
}

Are you still interested? I'll let you know if I find out how to solve the problem. Maybe somebody else has got any advice for us?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top