问题
I have updated from Hibernate 4.3 to the latest version, currently 5.2.10.Final.
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>
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>
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>
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)
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.
回答1:
Try https://github.com/Devskiller/jpa2ddl, this tool supports latest hibernate version.
Sample usage:
<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>
And invoke with command: mvn jpa2ddl:generate
来源:https://stackoverflow.com/questions/44306064/jpa2-1-hibernate-5-2-using-hibernate-tools-to-generate-ddl-via-an-ant-maven-tas