How to generate Envers database schema with org.hi

2020-06-21 07:49发布

I updated Hibernate to the 4.1.1.Final version. According to the documentation There are 2 ways to generate a database schema:

  1. Ant task org.hibernate.tool.ant.EnversHibernateToolTask.
  2. Run org.hibernate.tool.EnversSchemaGenerator from Java.

Hibernate-tools doesn't work with Hibernate-4.1.1.Final. It has a blocking bug.

I found only release notes and a test case. So how can I use org.hibernate.tool.EnversSchemaGenerator with my persistence.xml and Maven?

Update:

Found related thread on the Hibernate forum. It seems there is no answer to my question yet.

4条回答
The star\"
2楼-- · 2020-06-21 08:02

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>
查看更多
老娘就宠你
3楼-- · 2020-06-21 08:03

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);
}
查看更多
Explosion°爆炸
4楼-- · 2020-06-21 08:19

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.

查看更多
爷、活的狠高调
5楼-- · 2020-06-21 08:19

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?

查看更多
登录 后发表回答