Using Entities in Java EE Applications
  • EntityManagers are obtained via "Dependency Injection"
  • Within the context of an EJB method, the JTA transaction will be started for you
EJB Example  
  @Stateless
  public class EmployeeDaoImpl implements EmployeeDao {
    @PersistenceContext
    private EntityManager entityManager;

    public Collection getAllEmployees() {
      Query query = entityManager.createQuery( "select x from Employee x" );
      return query.getResultList();
    }
    ...
  }
  • In a servlet environment, you must initiate the JTA transactions yourself (a servlet filter maybe)
Servlet Example