Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get EntityManager from Hibernate session

I have built my application on Hibernate, but now I need to get EntityManager for one third-party module.

Could you please give me some hints how to get JPA objects from Hibernate?

(in my case I have SessionFactory and need EntityManager)

like image 398
jakub.petr Avatar asked Dec 11 '13 23:12

jakub.petr


People also ask

Does Hibernate use EntityManager?

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.

Which method is used to access the proprietary Hibernate Session from the EntityManager API?

unwrap(SessionFactory. class); In the first line, I get the current Hibernate Session from the EntityManager. I, therefore, call the unwrap method on the EntityManager and provide the Session class as a parameter.

How do I get EntityManager factory object?

Steps to persist an entity object. Persistence - The Persistence is a bootstrap class which is used to obtain an EntityManagerFactory interface. createEntityManagerFactory() method - The role of this method is to create and return an EntityManagerFactory for the named persistence unit.


1 Answers

I found this in Hibernate 5.2

 Session session = HibernateUtil.getSessionFactory().openSession();
 EntityManager em = session.getEntityManagerFactory().createEntityManager();
like image 139
Sunil Singh Bora Avatar answered Sep 29 '22 15:09

Sunil Singh Bora