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

10,542

Solution 1

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

Solution 2

Get an object of PersisterCreationContext and then try this :-

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

Pls check this link (Example 3.8. Native Bootstrapping - Putting it all together) to understand how to get standardRegistry, metadata and sessionFactory in Hibernate 5.x

Now as we were pulling metadata from persisterCreationContext and now we already had it so we can right away get the required PersistentClass object of any entity by

SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
PersistentClass persistentClass = metadata.getEntityBinding(className);

Solution 3

In Hibernate 3 and 4 you would do something like this

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 
    }

In Hibernate 5 initialise Metadata like this

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();

And use getEntityBindings() on metadata

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

Related videos on Youtube

Nirav Patel
Author by

Nirav Patel

Software Engineer working at Orbit Technologies, Ahmedabad, India

Updated on May 15, 2022

Comments

  • Nirav Patel
    Nirav Patel about 2 years

    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.

  • Nirav Patel
    Nirav Patel over 8 years
    How to retrieve persisterCreationContext? Can you please update answer with proper steps. i.e., normally we`ll have sessionFactory object or configuration object, so how can we retrieve PersisterCreationContext from it?
  • Vering
    Vering about 7 years
    How is the getSessiongFactoryBuilder method getting executed? So that I can call getMeta() to get the MetadataImplementor. Is EntityMetaData to be injected into Hibernate in some way? Sorry don't understand...
  • rxt66
    rxt66 over 3 years
    @NiravPatel@John Can you please share the complete solution for this problem as I am unable to implement.
  • Hamza Assada
    Hamza Assada over 2 years
    I did exactly that and even though I debugged it and made sure that this class is actually gets called, when I call EntityMetaData.get() it returns null, why??. Also when I replace ThreadLocal<MetadataImplementor> meta with just MetadataImplementor and directly assign it it works.