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

老子叫甜甜 提交于 2019-12-01 09:34:33

问题


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)


回答1:


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>



回答2:


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?



来源:https://stackoverflow.com/questions/12286727/how-can-i-generate-ddl-from-existing-entities-with-annotations-using-a-maven-plu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!