Devskiller / jpa2ddl

JPA Schema Generator Plugin
Apache License 2.0
109 stars 33 forks source link
hibernate jpa jpa-schema maven-plugin schema-migrations

Build Status Maven Central

JPA Schema Generator Plugin

Motivation

Why another tool to dump the JPA schema? All tools that we've found were related to legacy versions of Hibernate or were covering just simple cases, without options to configure dialect or naming strategy. Also all of the tools we've found are based on the SchemaExport class, which does not always correlate with the runtime schema - for example due to the lack of support for the Integrator services, used to register UserType classes like JodaTime or similar. We were also looking for a tool that is be able to handle further schema migrations, not just dump the current version.

Configuration parameters

Custom H2 dialects

H2 is often used to imitate native database engines, however with usage going beyond simple SQL it has huge limitations. To resolve some of them related to sequences we provide custom database dialects.

If you need different dialects please build them using above examples.

Maven Plugin

You can run this plugin directly or integrate it into the default build lifecycle.

Simple configuration example

<build>
    <plugins>
        <plugin>
            <groupId>com.devskiller.jpa2ddl</groupId>
            <artifactId>jpa2ddl-maven-plugin</artifactId>
            <version>0.9.12</version>
            <extensions>true</extensions> <!-- required to run automatically -->
            <configuration>
                <packages>
                    <package>com.test.model</package>
                </packages>
            </configuration>
        </plugin>
    </plugins>
</build>

Generate schema

<build>
    <plugins>
        <plugin>
            <groupId>com.devskiller.jpa2ddl</groupId>
            <artifactId>jpa2ddl-maven-plugin</artifactId>
            <version>0.9.12</version>
            <extensions>true</extensions> <!-- required to run automatically -->
            <configuration>
                <outputPath>${basedir}/src/main/resources/database.sql</outputPath>
                <packages>
                    <package>com.test.model</package>
                    <package>com.test.entities</package>
                </packages>
                <jpaProperties>
                    <property>
                        <name>hibernate.dialect</name>
                        <value>org.hibernate.dialect.MySQL57Dialect</value>
                    </property>
                    <property>
                        <name>hibernate.default_schema</name>
                        <value>prod</value>
                    </property>
                </jpaProperties>
                <formatOutput>true</formatOutput>
                <skipSequences>true</skipSequences>
                <delimiter>;</delimiter>
                <action>DROP_AND_CREATE</action>
            </configuration>
        </plugin>
    </plugins>
</build>

Generate migrations

It's also possible to generate automated migrations scripts. JPA2DDL supports Flyway naming patterns for versioned migrations.

All subsequent migration scripts are saved in the outputPath, in the following layout:

src/main/resources/migrations/
 v1__jpa2ddl.sql
 v2__jpa2ddl.sql
 ... next

Please note that after generation you can change the name of the file to make it more descriptive following the filename pattern v(N)__jpa2ddl(_custom_description).sql - for example v1__jpa2ddl_init.sql

Sample configuration:

<build>
    <plugins>
        <plugin>
            <groupId>com.devskiller.jpa2ddl</groupId>
            <artifactId>jpa2ddl-maven-plugin</artifactId>
            <version>0.9.12</version>
            <extensions>true</extensions> <!-- required to run automatically -->
            <configuration>
                <outputPath>${basedir}/src/main/resources/migrations/</outputPath>
                <packages>
                    <package>com.test.model</package>
                    <package>com.test.entities</package>
                </packages>
                <jpaProperties>
                    <property>
                        <name>hibernate.dialect</name>
                        <value>org.hibernate.dialect.MySQL57Dialect</value>
                    </property>
                </jpaProperties>
                <formatOutput>true</formatOutput>
                <delimiter>;</delimiter>
                <action>UPDATE</action>
            </configuration>
        </plugin>
    </plugins>
</build>

Direct invocation

./mvnw com.devskiller.jpa2ddl:jpa2ddl-maven-plugin:0.9.12:generate

Gradle Plugin

Below you can find a sample build.gradle script configuration:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "com.devskiller.jpa2ddl:jpa2ddl-gradle-plugin:0.9.12"
    }
}

apply plugin: 'com.devskiller.jpa2ddl'

jpa2ddl {
    packages = ['com.test.model']
}

Extending jpa2ddl with SchemaProcessors

Sometimes more actions that just saving the database migrations are needed. Example of such use case is when there is a need to generate QueryDSL or JOOQ mappings. The SchemaProcessor mechanism in jpa2ddl resolves such needs.

QueryDSL processor

Additional dependency jpa2ddl-querydsl-processor provides the processor to generate mappings for QueryDSL. To enable it:

 <build>
        <plugins>
            <plugin>
                <groupId>com.devskiller.jpa2ddl</groupId>
                <artifactId>jpa2ddl-maven-plugin</artifactId>
                <version>${project.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <packages>
                        <package>oss.devskiller.model</package>
                    </packages>
                    <action>UPDATE</action>
                    <processorProperties>
                        <property>
                            <name>queryDslOutputPath</name>
                            <value>${project.build.directory}/generated-sources/query-dsl</value>
                        </property>
                        <property>
                            <name>queryDslOutputPackage</name>
                            <value>oss.devskiller.querydsl</value>
                        </property>
                    </processorProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.devskiller.jpa2ddl</groupId>
                        <artifactId>jpa2ddl-querydsl-processor</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Building custom SchemaProcessors

It's possible to build and inject your custom schema processors to jpa2ddl. The only thing you need to do is to implement the com.devskiller.jpa2ddl.SchemaProcessor interface, and add the jar with our implementation as a dependency for the plugin.

Please refer to the jpa2ddl-querydsl-processor to see an example.