Tuesday, 19 May 2015

JPA Schema Generator

This is a sample of using the de.juplo plugin for generating a database schema from annotated JPA Entity classes.

The Config


 <plugin>
    <groupId>de.juplo</groupId>
    <artifactId>hibernate4-maven-plugin</artifactId>
    <version>1.1.0</version>
    <executions>
        <execution>
            <id>package</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>export</goal>
            </goals>
            <configuration>
                 <hibernateDialect>org.hibernate.dialect.MySQL5Dialect</hibernateDialect>
            </configuration>
        </execution>
        <execution>
            <id>test</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>export</goal>
            </goals>
            <configuration>
                <outputFile>${project.build.testOutputDirectory}/schema.sql</outputFile>
                <hibernateDialect>org.hibernate.dialect.H2Dialect</hibernateDialect>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <target>SCRIPT</target>
    </configuration>
 </plugin>

Note that the normal configuration doesn't need an outputFile because it is generated in the target directory by default.  This example uses two executions, the first for the standard target build and the second for a test build for use with H2 for unit testing.
The <target>SCRIPT</target> part means that a sql script is created rather than trying to execute this directly on a database.

For more information see http://juplo.de/hibernate4-maven-plugin/

No comments:

Post a comment