【发布时间】:2018-05-06 00:56:34
【问题描述】:
在从 Hibernate 4 迁移到 5 时,我遇到了 deprecation 并最终删除了 SchemaExport(Configuration) 构造函数。 Hibernate 5 中有什么好的替代方案?
用例
在测试期间,我们创建了一个 SchemaExport 实例,其配置具有一些属性集并定义了映射资源:
// somewhere else `Properties` are filled and passed as a parameter
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
// more properties ...
Configuration configuration = new Configuration();
configuration.setProperties(testProperties);
// parameter `String... mappingResources`
for (final String mapping : mappingResources) {
configuration.addResource(mapping);
}
// this doesn't compile
SchemaExport schemaExport = new SchemaExport(configuration);
最后一行在 Hibernate 5 上无法编译,因为构造函数已被删除。
选项
弃用建议使用SchemaExport(MetadataImplementor) 构造函数,但我很难找到创建MetadataImplementor 实例的好方法。我找到了一些选项,但在我看来它们都很可疑。
我能在 Hibernate 中找到的唯一具体实现是 MetadataImpl 和 InFlightMetadataCollectorImpl,但两者都在 org.hibernate.boot.internal 中,所以我认为我不应该使用它们。此外,MetadataImpl 有一个庞大的构造函数,我需要在其中提供每一个小细节,InFlightMetadataCollectorImpl 需要一个MetadataBuildingOptions,它与MetadataImplementor 有相同的问题(实现是内部的,难以构造)。
另外,看起来MetadataBuilderImpl 可能是构造MetadataImplementor 的便捷方式,但它也是内部的。
无论哪种方式,我都不知道如何在MetadataImplementor(或MetadataBuilderImpl)上设置Properties(或他们的条目)。
问题
创建SchemaExport 真的需要MetadataImplementor 吗?如果是这样,我如何从受支持的 API 中获取一个以及如何设置Properties?
最终我们想用execute 执行一个脚本,但这里的签名也发生了变化。我看到它现在需要ServiceRegistry,所以也许这是一条出路?
编辑
啊,我刚刚在 5.2(我想使用)中看到 SchemaExport 甚至不再使用 MetadataImplementor - 只有无参数的构造函数仍然存在。现在呢?
【问题讨论】: