本文介绍了使用Hibernate Tools的JPA2.1/Hibernate 5.2通过ANT Maven任务生成ddl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Hibernate 4.3更新到了最新版本,目前是5.2.10.Final.

I have updated from Hibernate 4.3 to the latest version, currently 5.2.10.Final.

我需要Maven生成ddl模式,以便当我运行放置数据库然后创建模式时,它将拾取生成的模式,这是我试图在maven中运行一个蚂蚁任务来运行的.在以前的版本中,我使用了...

I have the need for maven to generate the ddl schema so that when I run a drop database then create schema it will pick up the generated schmea which I am trying to get an ant task in maven to run. In my previous version I used...

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

我已按照此处的说明进行操作所以在我的pom.xml中,我有以下内容...

I have followed the instructions found here so within my pom.xml I now have the following...

<plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>generate-ddl-create</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <target name="schemaexport" depends="build-demo" description="Exports a generated schema to DB and file">
                              <taskdef
                                name="hibernatetool"
                                classname="org.hibernate.tool.ant.HibernateToolTask"
                                classpathref="maven.dependency.classpath"
                              />
                              <hibernatetool destdir=".">
                                <classpath>
                                  <path location="src/main/java"/>
                                </classpath>
                                <jpaconfiguration persistenceunit="randb_MariaDb" />
                                <hbm2ddl console="false" export="false" update="false" drop="false" create="true" outputfilename="/schema_MariaDb.sql" format="true" haltonerror="true"/>
                              </hibernatetool>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我的persistence.xml看起来像这样...

My persistence.xml looks like this...

<persistence-unit name="randb_MariaDb" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <!-- Comment this our if you want to use properties below -->
    <non-jta-data-source>java:/comp/env/jdbc/randb_MariaDb</non-jta-data-source>
    <properties>
        <!--
        <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/ran" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="r00tPwd" />
        <property name="hibernate.connection.shutdown" value="true" />
        <property name="hibernate.connection.pool_size" value="0"/>
        <property name="hibernate.connection.aggressive_release" value="true"/>
         -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.jdbc.batch_size" value="20"/>
    </properties>
</persistence-unit>

我的问题是,当我运行mvn antrun:run @ generate-ddl-create时,出现以下异常...

My problem is that when I run mvn antrun:run@generate-ddl-create I get the following exception...

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-       plugin:1.7:run (generate-ddl-create) on project WimacRanServer: An Ant BuildException has occured: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/comp/env/jdbc/randb_MariaDb]
[ERROR] around Ant part ...<hibernatetool destdir=".">... @ 5:30 in /Users/dave/gitRepository/WimacRanServer/target/antrun/build-main.xml: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (generate-ddl-create) on project WimacRanServer: An Ant BuildException has occured: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/comp/env/jdbc/randb_MariaDb]
around Ant part ...<hibernatetool destdir=".">... @ 5:30 in /Users/dave/gitRepository/WimacRanServer/target/antrun/build-main.xml
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)

我猜想这可能与非jta数据源有关,并且尝试了一些操作,例如删除该代码并取消注释其余属性,但无济于事.如果有人遇到过类似的问题或可以阐明一些想法,那么它将受到极大的欢迎.

I am guessing it may be something to do with the non-jta-data-source and have tried a few things like removing that and uncommenting the rest of the properties but to no avail. If anyone has come across a similar problem or can shed some light it would be greatly received.

推荐答案

尝试 https://github.com/Devskiller/jpa2ddl ,此工具支持最新的休眠版本.

Try https://github.com/Devskiller/jpa2ddl, this tool supports latest hibernate version.

样品用量:

<plugin>
    <groupId>com.devskiller.jpa2ddl</groupId>
    <artifactId>jpa2ddl-maven-plugin</artifactId>
    <version>0.9.5</version>
    <configuration>
        <packages>
            <package>com.test.model</package>
        </packages>
        <outputPath>${project.build.directory}/schema_MariaDb.sql</outputPath>
        <jpaProperties>
            <property>
                <name>hibernate.dialect</name>
                <value>org.hibernate.dialect.MySQL5InnoDBDialect</value>
            </property>
        </jpaProperties>
    </configuration>
</plugin>

并使用以下命令调用:mvn jpa2ddl:generate

And invoke with command: mvn jpa2ddl:generate

这篇关于使用Hibernate Tools的JPA2.1/Hibernate 5.2通过ANT Maven任务生成ddl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:43