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?

3
  • Did you solve this problem? What was your solution? Commented Sep 17, 2015 at 11:41
  • moving to JPA completely - it was easier than any of intermediate solutions we came up with. Commented Oct 5, 2015 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: stackoverflow.com/questions/32696237/… Commented Oct 5, 2015 at 12:07

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

The last part should be using the unwrap() method rather than explicit casting.
I thought it works, but unfortunatelly this doesn't supply the registered entities :/ I'm doomed for persistence.xml files with entities specified :/.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.