Reputation: 2981
I am new to JPA 2 and I want to find out which are the best practices for handling an EntityManager
on RESOURCE_LOCAL
and with JTA. From what I have read, I should be able to make a dependency injection, but I do not quite understand how.
I am using EclipseLink as an implementation.
Upvotes: 1
Views: 340
Reputation: 691
To obtain a reference to EntityManager in your bean, use the following annotation:
@PersistenceContext
private EntityManager entityManager;
Or if you are not using EJB:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myPersistenceUnitName");
EntityManager entityManager = entityManagerFactory.createEntityManager();
//Do some work...
entityManager.close();
entityManagerFactory.close();
See Persistence unit as RESOURCE_LOCAL or JTA? for an explanation of RESOURCE_LOCAL vs JTA.
Upvotes: 2