8

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?

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
psychollek
  • 323
  • 3
  • 10
  • Did you solve this problem? What was your solution? – Mike Nakis Sep 17 '15 at 11:41
  • moving to JPA completely - it was easier than any of intermediate solutions we came up with. – psychollek Oct 05 '15 at 11:29
  • Aha, so I suppose you are not using a hibernate interceptor then, right? I have this question which does not seem to attract any answers: http://stackoverflow.com/questions/32696237/jpa-with-hibernate-5-programmatically-create-entitymanagerfactory – Mike Nakis Oct 05 '15 at 12:07

1 Answers1

3

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();
}
MicSim
  • 26,265
  • 16
  • 90
  • 133
  • 2
    The last part should be using the unwrap() method rather than explicit casting. – Neil Stockton May 08 '15 at 17:33
  • I thought it works, but unfortunatelly this doesn't supply the registered entities :/ I'm doomed for persistence.xml files with entities specified :/. – psychollek May 14 '15 at 12:45
  • That's what I meant by: *"You will need a persistence.xml though, where you have defined a persistence unit for JPA."*. You somehow have to tell JPA which entities to persist. – MicSim May 17 '15 at 14:56