I have used annotation for using Entity Manager instead of using EntityManagerFactory.
@PersistenceContext
EntityManager entityManager;
I searched a lot regd the closing of entityManager. But in most places the EntityManager is being used
EntityManager em = EMF.get().createEntityManager();
And Im not sure how the closing varies for annotation based. Do we use it just like we use normal jdbc connection? Eg:
Connection conn=DataBaseConnection.getConnection();
PreparedStatement stmt;
ResultSet result;
stmt=conn.prepareStatement("SELECT * from table WHERE id = ? ");
stmt.setString(1,id);
result=stmt.executeQuery();
conn.close();
So, do we need to add a begin tran to the entityManager, commit it and then close it for each method we use? or will the annotation take care of it all?
public class someClass{
public someMethod1(){
//use entityManager - do I need to close it for each method?
}
public someMethod2(){
//use entityManager
}
}
Or am I getting this entirely wrong? Please advice.