[英]JPA2.1/ Hibernate 5.2 using Hibernate Tools to generate ddl via an ANT Maven task
[英]Generate DDL script at MAVEN build with Hibernate4 / JPA 2.1
看來用於生成DDL創建/刪除腳本的hibernate3-maven-plugin
與Hibernate 4.3
及更新版本(使用JPA 2.1
)不再兼容。
我使用這個插件配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>generate-sql-schema</id>
<phase>process-sources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
<configuration>
<hibernatetool>
<jpaconfiguration persistenceunit="${persistenceUnitName}" />
<hbm2ddl update="true" create="true" export="false"
outputfilename="src/main/sql/schema.sql" format="true"
console="true" />
</hibernatetool>
</configuration>
</execution>
</executions>
</plugin>
但是我收到以下錯誤:
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (generate-sql-schema) on project my-project: There was an error creating the AntRun task.
An Ant BuildException has occured: java.lang.NoClassDefFoundError: org/hibernate/util/ReflectHelper: org.hibernate.util.ReflectHelper -> [Help 1]
此類遷移到新包: org.hibernate.internal.util.ReflectHelper
但是我發現沒有明確的方法可以在MAVEN版本中繼續生成DDL創建腳本。
沒有hibernate4-maven-plugin
,或任何其他官方方式。
所以呢 ? 這不是一個應該支持的主要功能嗎? 怎么做 ?
由於Hibernate 4.3+
現在實現了JPA 2.1
因此生成DDL腳本的適當方法是使用以下一組JPA 2.1屬性:
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/>
可以在此處找到JPA 2.1中其他屬性和模式生成上下文的精彩摘要: https : //blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation
官方JPA 2.1規范在這里: https : //jcp.org/aboutJava/communityprocess/final/jsr338/index.html
由於這將在運行時生成,您可能希望在構建時執行此DDL生成 。
以下是以編程方式生成此腳本的JPA 2.1方法:
import java.io.IOException;
import java.util.Properties;
import javax.persistence.Persistence;
import org.hibernate.jpa.AvailableSettings;
public class JpaSchemaExport {
public static void main(String[] args) throws IOException {
execute(args[0], args[1]);
System.exit(0);
}
public static void execute(String persistenceUnitName, String destination) {
System.out.println("Generating DDL create script to : " + destination);
final Properties persistenceProperties = new Properties();
// XXX force persistence properties : remove database target
persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none");
// XXX force persistence properties : define create script target from metadata to destination
// persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination);
Persistence.generateSchema(persistenceUnitName, persistenceProperties);
}
}
你可以看到它非常簡單!
現在你可以在AntTask或MAVEN這樣的構建中使用它(對於MAVEN):
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>generate-ddl-create</id>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- ANT Task definition -->
<java classname="com.orange.tools.jpa.JpaSchemaExport"
fork="true" failonerror="true">
<arg value="${persistenceUnitName}" />
<arg value="target/jpa/sql/schema-create.sql" />
<!-- reference to the passed-in classpath reference -->
<classpath refid="maven.compile.classpath" />
</java>
</target>
</configuration>
</execution>
</executions>
</plugin>
請注意,官方的hibernate-maven-plugin也可能會或可能不會以某種方式執行此操作:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-maven-plugin</artifactId>
<version>4.3.1.Final</version>
</dependency>
請享用 ! :)
我可能有點晚了,但Andrew Thompson有另一個不涉及persistence.xml或真實數據庫的解決方案,盡管它特定於Hibernate。
http://jandrewthompson.blogspot.com/2009/10/how-to-generate-ddl-scripts-from.html
可悲的是,它涉及一些代碼,但它非常靈活。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.