【发布时间】:2018-05-09 09:16:52
【问题描述】:
长期以来,我在 java 中有一个小型应用程序,它使用 hibernate SchemaExport 来获取文件中的所有实际数据库结构。这在 Hibernate 4.X 中运行良好。
基本上我在 java Main.class 中执行:
hibernateConfiguration.setProperty("hibernate.hbm2ddl.auto", "create");
hibernateConfiguration.setProperty("hibernate.dialect", dialect.getDialectClass());
hibernateConfiguration.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/"
SchemaExport export = new SchemaExport(hibernateConfiguration);
export.setDelimiter(";");
export.setOutputFile(outputFile);
export.setFormat(true);
export.execute(false, false, false, true);
每次项目执行时我都会使用exec-maven-plugin启动它:
<!-- Creates the database script BEFORE testing -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.schemaexporter.main</mainClass>
<!-- <skip>true</skip> -->
<arguments>
[...] <!-- Some database connection parameters -->
</arguments>
</configuration>
</plugin>
现在,我刚刚更新到 Hibernate 5 (5.2.17.Final)。为此,我将代码更新为:
MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.hbm2ddl.auto", "create")
.applySetting("hibernate.connection.driver_class", dialect.getDriver())
.applySetting("hibernate.dialect", dialect.getDialectClass())
.applySetting("hibernate.connection.driver_class", dialect.getDriver())
.applySetting("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/" + databaseName)
.applySetting("hibernate.connection.username", username)
.applySetting("hibernate.connection.password", password).build());
SchemaExport export = new SchemaExport();
export.setDelimiter(";");
export.setOutputFile(directory + File.separator + outputFile);
export.setFormat(true);
export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata.buildMetadata());
数据库脚本已正确创建。但exec-maven-process 挂起,无法继续执行其他操作。对于挂起,我指的是 maven 进程永远不会结束并且不会继续下一个阶段(执行单一测试)。
到目前为止我所尝试的:
- 添加到
exec-maven-plugin选项<async>true</async>但没有任何变化。 - 将
System.exit(0)添加到 Main 类,但 maven 已被杀死并且不会继续到下一个阶段。 - 在 bash 中创建一个运行脚本,作为suggested here,进程返回
Async process complete, exit value = 0,但没有生成数据库脚本。也许我可以更深入地研究脚本以找到错误,但这不是我的首选方式。
不过,我还是不明白为什么将 Hibernate 4 更改为 Hibernate 5 会导致进程无法结束。我检查了代码(到处都是基本的System.out),所有行都正确执行到最后,但过程仍然有效。
有谁知道 Hibernate 5 的行为变化是否会导致这种不良行为?
【问题讨论】: