gpt4 book ai didi

java - JPA2.1/Hibernate 5.2 使用 Hibernate 工具通过 ANT Maven 任务生成 ddl

转载 作者:行者123 更新时间:2023-11-30 07:54:07 26 4
gpt4 key购买 nike

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

我需要 maven 生成 ddl 模式,这样当我运行一个删除数据库然后创建模式时,它将选择生成的 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:run@generate-ddl-create 时,出现以下异常...

[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 数据源有关,并尝试了一些方法,例如删除它并取消对其余属性的注释,但无济于事。如果有人遇到过类似的问题或可以阐明一些问题,那将不胜感激。

最佳答案

尝试 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

关于java - JPA2.1/Hibernate 5.2 使用 Hibernate 工具通过 ANT Maven 任务生成 ddl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44306064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com