简体   繁体   中英

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

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.

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

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

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
  }

This is discussed in the Hibernate 5.0 Migration Guide as well as the Bootstrap chapter in the Hibernate User Guide (see especially the "Legacy Bootstrapping" appendix).

In short, while Configuration is still supported for straight-line bootstrapping, if you want to "hook into" the bootstrap process you are going to have to use the new bootstrap APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM