方法 org.hibernate.cfg.Configuration.getClassMapping(className) 从 4.3.x 到 5.x 的 Hibernate 迁移

Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cfg.Configuration.getClassMapping(className)

在Hibernate 4.3.x中,有classorg.hibernate.cfg.Configuration的方法getClassMapping(className)。 但在 Hibernate 5.x 中,此 getClassMapping(className) 方法已从 Configuration class 中删除。

Hibernate-5 中的代码替换是什么?

请帮助解决此迁移问题。

获取 PersisterCreationContext 的对象然后试试这个:-

PersistentClass persistentClass = 
persisterCreationContext.getMetadata().getEntityBinding(className);

请检查此 link示例 3.8。本机引导 - 将它们放在一起)以了解如何在 [=12= 中获取标准注册表、元数据和会话工厂]

现在我们从 persisterCreationContext 中提取元数据,现在我们已经有了它,所以我们可以立即通过

获得任何实体所需的 PersistentClass 对象
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
PersistentClass persistentClass = metadata.getEntityBinding(className);

posted 到 Broadleaf Commerce 因为他们也需要 PersistentClass:

I've been tooling with Hibernate 5, and some of these changes .... To get metadata now use the Serviceloader:

package org.broadleafcommerce.openadmin.server.dao;

import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.boot.spi.SessionFactoryBuilderFactory;
import org.hibernate.boot.spi.SessionFactoryBuilderImplementor;

public class EntityMetaData implements SessionFactoryBuilderFactory {

    private static final ThreadLocal<MetadataImplementor> meta = new ThreadLocal<>();

    @Override
    public SessionFactoryBuilder getSessionFactoryBuilder(MetadataImplementor metadata, SessionFactoryBuilderImplementor defaultBuilder) {
        meta.set(metadata);
        return defaultBuilder;
    }

    public static MetadataImplementor getMeta() {
        return meta.get();
    }
}

您将需要文件:

/resources/META-INF/services/org.hibernate.boot.spi.SessionFactoryBuilderFactory

使用完全限定的 class 名称,在我的示例中是:

org.broadleafcommerce.openadmin.server.dao.EntityMetaData

Hibernate 5.0 Migration Guide as well as the Bootstrap chapter in the Hibernate User Guide (see especially the "Legacy Bootstrapping" 附录对此进行了讨论。

简而言之,虽然直线 bootstrapping 仍然支持配置,但如果您想 "hook into" bootstrap 过程,您将不得不使用新的 bootstrap API。

在 Hibernate 3 和 4 中你会做这样的事情

URL configFileURL = getResource(); //some method to get hold of the location of your hibernate.cfg.xml
Configuration configuration = (new Configuration()).configure(configFileURL);
Iterator classMappings = configuration.getClassMappings();
  while (classMappings.hasNext()) {
    PersistentClass persistentClass = (PersistentClass) classMappings.next();
    //do somthing 
    }

在 Hibernate 5 中像这样初始化元数据

URL configFileURL = getResource(); //some method to get hold of the location of your hibernate.cfg.xml
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFileURL).build();
Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();

并对元数据使用 getEntityBindings()

Collection<PersistentClass> entityBindings = metadata.getEntityBindings();
Iterator<PersistentClass> iterator = entityBindings.iterator();
  while (iterator.hasNext()) {
    PersistentClass persistentClass = iterator.next();    
    //do somthing
  }