【问题标题】:Generate DDL script at MAVEN build with Hibernate4 / JPA 2.1使用 Hibernate4 / JPA 2.1 在 MAVEN 构建中生成 DDL 脚本
【发布时间】:2015-02-03 12:30:49
【问题描述】:

用于生成 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,或任何其他官方方式。

那又怎样?这不是应该支持的主要功能吗?怎么办?

【问题讨论】:

标签: maven ddl hibernate-4.x jpa-2.1 hibernate3-maven-plugin


【解决方案1】:

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

享受吧! :)

【讨论】:

  • 谢谢@ruckc,但你有没有像 wypieprz 所说的那样尝试mvnrepository.com/artifact/org.hibernate/hibernate-maven-plugin/…
  • 嗨。我已经用 gradle 和没有 persistence.xml 文件尝试了你的解决方案,但我遇到了问题。我打开了一个单独的帖子:stackoverflow.com/questions/30225183。有人可以帮忙吗?
  • 只有一个问题:如果你设法让它工作,你如何运行 maven 插件?是否需要启动 entitymanager 才能进行生成?
  • @balteo 我认为实体管理器会自动启动以生成 JPA 模式。正如示例所指定的,当您启动(例如)“mvn clean install”命令时,maven 插件在“process-classes”阶段执行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2021-06-01
  • 1970-01-01
  • 2015-06-20
  • 2011-08-24
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多