I am writing a CDI-JPA DAO pattern that not using EJB because I am using Tomcat. Here is my code:
@ApplicationScoped
public class UserDao {
@PersistenceContext(unitName = "unitName1")
EntityManager entityManager;
public void saveUser(User user) {
this.entityManager.persist(user);
}
public void removeUser(User user) {
this.entityManager.remove(user);
}
public void getUser(int id) {
this.entityManager.find(User.class, id);
}
}
Since all my DAO classes are annotated with @ApplicationScoped
so I was wondering whether IT IS SAFE to inject entityManager using @PersistenceContext
as I did? Can someone tell me is that ok? If NOT, please give me your ideas.