views:

52

answers:

1

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

+1  A: 

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