2017-06-01 128 views
2

我已经从Hibernate 4.3更新到最新版本,当前是5.2.10.Final。JPA2.1/Hibernate 5.2使用Hibernate Tools通过ANT生成ddl Maven任务

我需要maven生成ddl模式,以便当我运行drop database然后创建模式时,它会选择生成的schmea,我试图在maven中运行一个ant任务。在我以前的版本,我用...

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

我也跟着指示找到here所以我的pom.xml内我现在有以下...

<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样子这...

<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:运行@产生-DDL创建我得到下面的异常...

[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数据源有关,并尝试了一些东西,如删除和取消注释其余属性,但无济于事。如果有人遇到过类似的问题或者可以发现一些问题,那么将会大受欢迎。

+0

与休眠5你并不需要在所有 – XtremeBaumer

+0

我的文档阅读,你没有的persistence.xml提供一个persistence.xml,但从我的理解你仍然可以。这个失败的例子之前在升级hibernate之前使用mvn hibernate3:hbm2ddl工作过,我试图回到一个我可以使用类似命令的地步,就像我提到的在mavn构建中生成模式一样。 –

+0

我自己从来没有使用ANT,但我知道的是,如果hibernate不存在,hibernate会在指定的数据库中生成一个表。对于hibernate4程序,我有一个persistence.xml以及一个application.xml,用于定义数据库连接的弹簧 – XtremeBaumer

回答

3

尝试https://github.com/Devskiller/jpa2ddl,该工具支持最新的hibernate版本。

使用范例:

<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

+0

感谢您的快速答复我已经在其他事情...我看了一下github项目,并注意到序列跳过被添加。我试图使用它,但它仍然生成像这样的序列 'create table UserSeq( next_val bigint )engine = InnoDB; 插入UserSeq值(2);'我也尝试'hibernate.id.new_generator_mappings = false'无济于事。我不认为你有任何其他建议? –

+0

@DavidRowntree你使用的是最新版本0.9.6吗?如果是的话,请您在存储库中创建问题并提供一些示例代码? –

+2

@JakubKubrynski正是这样,非常感谢你非常感谢,伟大而有用的工具。 –