Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating EntityManagerFactory from hibernate Configuration

In our current application (Java SE) we use Hibernate specific API, but we kind of want to migrate to JPA wherever possible (but slowly). For that, I need EntityManagerFactory instead of SessionFactory (and I would like to keep this an axiom without dispute).

Where is the problem is, that currently our session factory is being created from org.hibernate.cfg.Configuration and I would like to keep it as it for now - as this configuration is passed thru different parts of our software which can and do configure the persistence as they want.

So the question is: how can I make

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                                   .applySettings( hibConfiguration.getProperties() )
                                   .buildServiceRegistry();
SessionFactory sessionFactory = hibConfiguration.buildSessionFactory( serviceRegistry );

equivalent resulting in EntityManagerFactory?

like image 239
psychollek Avatar asked May 08 '15 13:05

psychollek


People also ask

What is Hibernate EntityManagerFactory?

Hibernate provides implementation of JPA interfaces EntityManagerFactory and EntityManager . EntityManagerFactory provides instances of EntityManager for connecting to same database. All the instances are configured to use the same setting as defined by the default implementation.

What is difference between EntityManagerFactory and EntityManager?

EntityManagerFactory vs EntityManager EntityManager: whenever using spring avoid managing/using EntityManagerFactory since Spring manages concurreny for you. The entity manger injected by @PersistenceContext is thread safe. While EntityManagerFactory instances are thread-safe, EntityManager instances are not.

What is EntityManagerFactory?

EntityManagerFactory class is a factory for EntityManager s. EntityManager : The javax. persistence. EntityManager is the primary JPA interface used by applications. Each EntityManager manages a set of persistent objects, and has APIs to insert new objects and delete existing ones.


1 Answers

This is quite straightforward. You will need a persistence.xml though, where you have defined a persistence unit for JPA. Then you have to convert the Hibernate properties to a Map, so you can pass them to the createEntityManagerFactory method. This will give you the EntityManagerFactory using your Hibernate properties.

public EntityManagerFactory createEntityManagerFactory(Configuration hibConfiguration) {
    Properties p = hibConfiguration.getProperties();

    // convert to Map
    Map<String, String> pMap = new HashMap<>();
    Enumeration<?> e = p.propertyNames();
    while (e.hasMoreElements()) {
        String s = (String) e.nextElement();
        pMap.put(s, p.getProperty(s));
    }

    // create EntityManagerFactory
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("some persistence unit", pMap);

    return emf;
}   

If you need the SessionFactory from the EntityManagerFactory (the other way around), then you can use this method:

public SessionFactory getSessionFactory(EntityManagerFactory entityManagerFactory) {
    return ((EntityManagerFactoryImpl) entityManagerFactory).getSessionFactory();
}
like image 145
MicSim Avatar answered Oct 11 '22 02:10

MicSim