在Hibernate 5中替换SchemaExport(Configuration) [英] Replacing SchemaExport(Configuration) in Hibernate 5

查看:244
本文介绍了在Hibernate 5中替换SchemaExport(Configuration)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Hibernate 4迁移到5时,我遇到了

While migrating from Hibernate 4 to 5 I came across the deprecation and eventual removal of the SchemaExport(Configuration) constructor. What is a good alternative in Hibernate 5?

在测试过程中,我们使用配置了一些属性并定义映射资源的配置创建了SchemaExport实例:

During testing we create a SchemaExport instance with a configuration that had some properties set and defines mapping resources:

// 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上编译.

The last line does not compile on Hibernate 5 because the constructor was removed.

不建议使用SchemaExport(MetadataImplementor)构造函数,但是我很难找到创建MetadataImplementor实例的好方法.我找到了一些选择,但是它们对我来说似乎都是腥.

The deprecation recommends to use the SchemaExport(MetadataImplementor) constructor, but I struggle to find a good way to create a MetadataImplementor instance. I found a few options, but they all look fishy to me.

我可以在Hibernate中找到的唯一具体实现是MetadataImplInFlightMetadataCollectorImpl,但是都在org.hibernate.boot.internal中,所以我假设我不应该使用它们.此外,MetadataImpl具有庞大的构造函数,其中我需要提供每个小细节,而InFlightMetadataCollectorImpl需要一个MetadataBuildingOptions,它与MetadataImplementor一样具有相同的问题(实现是内部的,难以构造).

The only concrete implementations in Hibernate that I could find are MetadataImpl and InFlightMetadataCollectorImpl, but both are in org.hibernate.boot.internal, so I assume I'm not supposed to use them. Also, MetadataImpl has a massive constructor in which I need to provide every little detail and InFlightMetadataCollectorImpl needs a MetadataBuildingOptions, which has the same problems as MetadataImplementor (implementation is internal and hard to construct).

或者,看起来MetadataBuilderImpl可能是构造MetadataImplementor的便捷方法,但它也是内部的.

Alternatively, it looks like MetadataBuilderImpl might be a handy way to construct a MetadataImplementor, but it's also internal.

无论哪种方式,我都找不到如何在MetadataImplementor(或MetadataBuilderImpl)上设置Properties(或其条目)的方法.

Either way, I couldn't find out how to set Properties (or their entries) on a MetadataImplementor (or MetadataBuilderImpl for that matter).

创建SchemaExport确实需要MetadataImplementor吗?如果是这样,如何从受支持的API中获取一个?如何设置Properties?

Is a MetadataImplementor really required to create a SchemaExport? If so, how do I get one from a supported API and how do I set Properties?

最终,我们想使用execute执行脚本,但是签名也在此处更改.我看到它现在需要一个ServiceRegistry,所以也许这是一个出路?

Eventually we want to execute a script with execute, but the signature changed here as well. I see it now takes a ServiceRegistry, so maybe that would be a way out?

Argh,我刚刚看到在5.2(我想使用)中,SchemaExport甚至不再使用MetadataImplementor-仅保留了无参数构造函数.现在怎么办?

Argh, I just saw that in 5.2 (which I want to use) SchemaExport does not even take a MetadataImplementor any longer - only the parameterless constructor remains. What now?

推荐答案

从其他答案中提炼出来,我必须执行以下操作:

Distilling from other answers, I had to do the followings:

        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
            .applySetting("hibernate.hbm2ddl.auto", "create")
            .applySetting("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
            .applySetting("hibernate.id.new_generator_mappings", "true")
            .build();
    MetadataSources sources = new MetadataSources(standardRegistry);
    managedClassNames.forEach(sources::addAnnotatedClass);
    MetadataImplementor metadata = (MetadataImplementor) sources
            .getMetadataBuilder()
            .build();

    SchemaExport export = new SchemaExport(metadata);

希望有帮助

这篇关于在Hibernate 5中替换SchemaExport(Configuration)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆