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

Nirav Patel picture Nirav Patel · Sep 25, 2015 · Viewed 9.9k times · Source

In Hibernate 4.3.x, there is a method getClassMapping(className) of class org.hibernate.cfg.Configuration. But in Hibernate 5.x, this getClassMapping(className) method is removed from Configuration class.

What will be the code substitution in Hibernate-5?

Please help on this migration issue.

Answer

John picture John · Oct 17, 2015

I posted to Broadleaf Commerce because they also needed 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();
    }
}

You will need the file:

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

with the fully qualified class name, which in my example is:

org.broadleafcommerce.openadmin.server.dao.EntityMetaData