【问题标题】:Unable to generate hbm2ddl using hibernate3-maven-plugin-3.0无法使用 hibernate3-maven-plugin-3.0 生成 hbm2ddl
【发布时间】:2012-03-05 19:35:29
【问题描述】:

我已更新到更新版本的 hibernate3-maven-plugin。尝试使用下面提到的插件时出现以下错误。

不胜感激任何解决此问题的指针。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>3.0</version>

    <executions>
        <execution>
            <id>create sql schema</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>hbm2ddl</goal>
            </goals>
            <configuration>
                <componentProperties>
                    <persistenceunit>${app.module}</persistenceunit>
                    <drop>false</drop>
                    <create>true</create>
                    <outputfilename>${app.sql}-create.sql</outputfilename>
                    <skip>${db.schema.gen.skip}</skip>
                </componentProperties>
            </configuration>
        </execution>

        <execution>
            <id>drop sql schema</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>hbm2ddl</goal>
            </goals>
            <configuration>
                <componentProperties>
                    <persistenceunit>${app.module}</persistenceunit>
                    <drop>true</drop>
                    <create>false</create>
                    <outputfilename>${app.sql}-drop.sql</outputfilename>
                    <skip>${db.schema.gen.skip}</skip>
                </componentProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project sample: There was an error creating the AntRun task. NullPointerException -> [Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project framework: There was an error creating the AntRun task.

【问题讨论】:

    标签: hibernate hbm2ddl


    【解决方案1】:

    配置方式改为直接使用ant hibernate工具插件。所以配置与ant插件的格式完全相同,不需要额外的taskDef,例如jpa配置。有关更多信息,请参阅 hibernate ant 工具参考文档:http://docs.jboss.org/tools/3.3.0.Final/en/hibernatetools/html_single/index.html#d0e4651

    对于带有 jpa 配置的 hbm2ddl,您可以使用以下内容:

    <plugin>
        <!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
    
        <configuration>
            <hibernatetool>
                <jpaconfiguration persistenceunit="unitname" />
    
                <hbm2ddl export="false" create="true"
                    update="true" format="true" outputfilename="schemaDiff.ddl" />
    
            </hibernatetool>
        </configuration>
    </plugin>
    

    失败时有配置休眠工具的“target/antrun/build-main.xml”文件。对于上面的示例,如下所示:

    <?xml version="1.0" encoding="UTF-8" ?>
    <project name="maven-antrun-" default="main"  >
    <target name="main">
      <taskdef classname="org.hibernate.tool.ant.EnversHibernateToolTask" name="hibernatetool"/>
      <mkdir dir="/home/xxx/workspace/projectname/target/sql/hibernate3"/>
      <hibernatetool destdir="/home/xxx/workspace/projectname/target/sql/hibernate3">
        <jpaconfiguration persistenceunit="schemaDiff"/>
        <hbm2ddl update="true" export="false" outputfilename="schemaDiff.ddl" format=
    "true" create="true"/>
      </hibernatetool>
    </target>
    </project>
    

    【讨论】:

    • 这似乎仍然没有什么区别,我观察到和以前一样的错误。
    • 相关的调试信息可以为解决这个问题提供更多的提示。只需在 maven 命令中添加“-X”或“-debug”即可。
    • 请粘贴target/antrun/build-main.xml文件的内容,让我们看看ant任务是如何配置的。请参阅我编辑的答案以进行第一次比较。
    【解决方案2】:

    我也遇到了同样的问题,最后通过这个例子 (http://www.celinio.net/techblog/?p=1125) 和单独为插件指定休眠 deps 解决了这个问题。这是因为在我的情况下,我有一个单独的域对象模块,它只使用 JPA2(没有特定的休眠引用),所以我需要为 DDL 生成拉入 deps,而不用担心它们会影响这个模块的依赖项。

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <components>
                        <component>
                            <name>hbm2ddl</name>
                            <implementation>jpaconfiguration</implementation>
                        </component>
                    </components>
                    <hibernatetool>
                        <classpath>
                            <path location="${project.build.directory}/classes" />
                            <path location="${project.basedir}/src/main/resources/META-INF/" />
                        </classpath>
                        <jpaconfiguration persistenceunit="Configuration" />
                        <hbm2ddl create="true" export="false" drop="true"
                            outputfilename="configuration.sql" format="true" console="true" />
                    </hibernatetool>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate.javax.persistence</groupId>
                        <artifactId>hibernate-jpa-2.0-api</artifactId>
                        <version>1.0.0.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>3.6.7.Final</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 这个解决方案在我添加 hibernate-jpa-2.0-api:1.0.0.Final, hibernate-entitymanager:3.6.8.Final, hibernate-core:3.6.8.Final, hibernate-validator:4.2.0.Final in dependencies
    【解决方案3】:

    我已经使它工作如下:

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>3.0</version>
    <executions>
        <execution>
            <id>create-schema</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <hibernatetool destdir="${project.basedir}">
                    <classpath>
                        <path
                            location="${project.basedir}/src/main/resources/mappings/" />
                    </classpath>
                    <configuration
                        configurationfile="${project.basedir}/src/test/resources/hibernate.cfg.xml" />
                    <hbm2ddl create="true" export="false"
                        drop="true" outputfilename="schema.sql"
                        format="true" console="false" />
                </hibernatetool>
            </configuration>
        </execution>
    </executions>
    

    基本思想是使用目标'run',然后配置hibernatetool来运行你想要的导出器。因此,您可以通过在标签内添加更多导出器配置来一次性运行多个导出器。如需更多信息,请查看此处http://docs.jboss.org/tools/2.0.0.GA/hibernatetools/en/html_single/index.htmlhttp://mojo.codehaus.org/hibernate3-maven-plugin/examples/run-multiple-goals.html。 我花了几个小时才弄清楚。希望有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-10-19
      • 2011-04-19
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2011-02-19
      • 2011-02-27
      • 2014-06-09
      相关资源
      最近更新 更多