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

I have updated Hibernate to 4.1.1.Final version. According to the documentation There are two ways to create a database schema:

  • Ant task org.hibernate.tool.ant.EnversHibernateToolTask

    .
  • Run org.hibernate.tool.EnversSchemaGenerator

    from Java.

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

I only found the release notes and a test case , So how can I use org.hibernate.tool.EnversSchemaGenerator

with my persistence.xml and Maven?

Update:

Found it linked in the Hibernate forum . There seems to be no answer to my question yet.

+3


source to share


4 answers


Yuplo created a Maven plugin for Hibernate 4 . Plugin supports schema export including Envers. Below is a working example. Check the official plugin configuration documentation for an explanation of the options used.

The plugin generates a file schema.sql

in the Maven directory /target

for the target test

. Or you can manually run the target hibernate4:export

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>

      

+8


source


You don't need Ant or Hibernate tools. It is very easy to just use EnversSchemaGenerator like:

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 specify a filename to write, but the above code will print to syslog anyway.

+4


source


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

      

+2


source


I have the same problem. There is now a version of Hibernate 4 Tools:

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

      

But this Ant snippet does not export audit tables, only the "main" 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 thing with this code: Only "base" tables, 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 curious? I'll let you know, find out how to fix the problem. Maybe someone has some advice for us?

0


source







All Articles