I need comments on my JPA code. Am I doing it correctly? I need some advice on best practices in Java.
I have this method below:
public Comics add(Comics comics) {
EntityManager em = EMF.get().createEntityManager();
EntityTransaction tx = null;
try{
tx = em.getTransaction();
tx.begin();
em.persist(comics);
tx.commit();
}catch(Exception ex){
ex.printStackTrace();
if(tx != null && tx.isActive())
tx.rollback();
} finally{
em.close();
}
return comics;
}
When I called the method, I just have:
ComicsAccess access = new ComicsAccess();
access.add( comics );
I got no error on the above code, what I want are some suggestions on my coding style. Is the above good code if used in production?