如何使用maven插件从带有注释的现有实体生成DDL?

时间:2012-09-05 17:26:25

标签: java java-ee maven ejb jpa-2.0

我有maven项目,我想从现有实体生成DDL。

我该怎么做?

我是否有可以生成DDL的maven插件?

我正在使用JPA。(打开jpa)

2 个答案:

答案 0 :(得分:3)

openjpa-maven-plugin插件提供目标sql。使用此目标,可以从现有实体创建DDL。

<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>

答案 1 :(得分:2)

如果您使用Hibernate作为JPA提供程序,请检查http://users.mafr.de/~matthias/articles/generating-ddl-scripts.html

generate DDL from JPA annotations可能重复,虽然问题的措辞略有不同?