There are two types of entity managers available, depending on the Java environment in which the entity manager is obtained.
In the Java EE environment, the container manages the entity manager. The entity manager can be injected into a session bean, servlet, or JSP using dependency injection with the
@PersistenceContext annotation, as shown next:
@PersistenceContext public EntityManager em;
Alternatively, the entity manager can be looked up using JNDI in the environment referencing context:
@PersistenceContext(name="CatalogEM", unitName="em") @Resource SessionContext ctx; EntityManager em = (EntityManager)ctx.lookup("CatalogEM");
We shall be using the simpler of the two methods—the dependency injection method. Transactions define when entities are synchronized with the database. Container-managed entity managers always use JTA transactions, the transactions of the Java EE server.