0

Can I create an EntityManager from EntityManagerFactory outside a bean. If so, how would I do it?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Raj
  • 147
  • 4
  • 15

1 Answers1

2

In a non-managed environment (this is what you mean by outside a bean, right?), then you typically use:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu"); 
EntityManager em = emf.createEntityManager();
em.getTransaction().begin()
...
em.getTransaction().commit();
emf.close();

Check the other factory method allowing to pass properties as parameter (they will override any values that may have been configured elsewhere): Persistence.createEntityManagerFactory(String, Map).

See also

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124