Kayser
Kayser

Reputation: 6694

How can I generate DDL from existing entities with annotations using a maven plugin?

I have maven project and I want to generate DDL from existing entities.

How can I do that?

Is there any maven plugin that I can generate the DDL?

I am using JPA.(open jpa)

Upvotes: 2

Views: 4570

Answers (2)

Kayser
Kayser

Reputation: 6694

openjpa-maven-plugin plugin provides a goal sql. Using this goal, it is possible to create the DDL from existing entities.

<pluginManagement>
<plugin>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <version>2.2.0</version>
    <configuration>
        <includes>**/entity/ *.class</includes>
        <addDefaultConstructor>true</addDefaultConstructor>
        <connectionDriverName>com.ibm.db2.jcc.DB2Driver</connectionDriverName>
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
        <persistenceXmlFile>${basedir}/src/main/resources/META-INF/persistence.xml</persistenceXmlFile>
        <skip>${skip.jpa}</skip>
        <sqlFile>${basedir}/src/main/resources/database.sql</sqlFile>
    </configuration>
        <dependencies>
            <dependency>
                 <groupId>org.apache.openjpa</groupId>
                 <artifactId>openjpa</artifactId>
                 <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.6</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.6.6</version>
            </dependency>
        </dependencies>
    </plugin>
</pluginManagement>

<plugin>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>sql</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>sql</goal>
            </goals>
        </execution>                    
    </executions>
</plugin>

Upvotes: 3

Integrating Stuff
Integrating Stuff

Reputation: 5303

If you are using Hibernate as your JPA provider, check http://users.mafr.de/~matthias/articles/generating-ddl-scripts.html.

Possible duplicate of generate DDL from JPA annotations, although the question there is phrased slightly differently?

Upvotes: 2

Related Questions