I need a EntityManager
for my some methods in Spring Data JPA. What best method for enabling this in my code:
@Service
public class MyService {
private final EntityManager em;
public MyService(EntityManager em) {
this.em = em;
}
}
Or that:
@Service
public class MyService {
@PersistenceContext
private EntityManager em;
}
I like first method, but I know that JPA specification declare closing EntityManager
after transaction, and in first example field is final (how it can injecting again after em.close
, if object is immutable)?