0

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.