首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Hibernate4 /JPA2.1在MAVEN构建时生成DDL脚本

使用Hibernate4 /JPA2.1在MAVEN构建时生成DDL脚本
EN

Stack Overflow用户
提问于 2014-12-05 18:47:49
回答 1查看 15.6K关注 0票数 10

似乎用于生成DDL /drop脚本的hibernate3-maven-pluginHibernate 4.3和更新版本(使用JPA 2.1)不再兼容。

我使用这个插件配置:

代码语言:javascript
复制
            <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>

但我得到以下错误:

代码语言:javascript
复制
[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 build中没有明确的方法来生成DDL create脚本。

没有hibernate4-maven-plugin,或者任何其他官方的方法。

那又怎样?这难道不是应该支持的主要功能吗?该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-05 18:47:49

由于Hibernate 4.3+现在实现了JPA 2.1,因此生成DDL脚本的适当方式是使用以下一组JPA2.1属性:

代码语言:javascript
复制
<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"/>

JPA2.1中的模式生成的其他属性和上下文的一个很好的总结可以在这里找到:https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation

和官方JPA2.1规范在这里:https://jcp.org/aboutJava/communityprocess/final/jsr338/index.html

由于这将在运行时生成,因此您可能希望在build中执行此DDL生成。

以下是以编程方式生成此脚本的JPA 2.1方法:

代码语言:javascript
复制
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):

代码语言:javascript
复制
<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也可能会,也可能不会,以某种方式做到这一点:

代码语言:javascript
复制
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-maven-plugin</artifactId>
    <version>4.3.1.Final</version>
</dependency>

尽情享受!:)

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27314165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档