I am starting to learn JPA and EJB, and when I deal with some examples, I saw there are two kinds of creating EntityManager
, using @PersistenceContext
and using EntityManagerFactory
:
@PersistenceContext(unitName ="somename")
private EntityManager entityManager;
and
EntityManagerFactory emf = Persistence.createEntityManagerFactory("somename");
EntityManager em = emf.createEntityManager();
I am not really clear if these two approaches are the same or not. Can anyone explain me what are the differences between them and if they are the same, which one is "newer" nowadays for creating JavaEE Applications.
Thank you.