How to generate Envers database schema with org.hibernate.tool.EnversSchemaGenerator?

只谈情不闲聊 提交于 2019-12-03 16:48:43

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!