One day I was working with JPA, however I noticed that I had to repeat the process of creating an EntityManagerFactory and EntityManager object in all the DAO (Data Access Object) classes. There after, I had to open begin and commit a transaction every time I had to save and or update the object.
Due to repetition of such code, I become bold.
Certainly I thought it would be better to centralize all the JPA 'boiler' code in one class with the use of generic methods.
When using JPA, to obtain an entity, one has to call find method on the EntityManager as shown below
entityManager.find(MyClass.lass,keyFieldValue) ;
To avoid passing the 'concrete' class, I had to create a generic method for find method in my utility class as shown below
public <T> T find(Class<T> entity, Object param){ EntityManager em=entityManagerFactory.createEntityManager(); try{ return em.find(entity,param); }finally{ if(em.isOpen()){ em.close(); } } }
Therefore, I would reuse the method without worry of casting problems, as it would be if I had used and Object class. Below is a complete complete class
import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; public class JpaUtil { private EntityManagerFactory entityManagerFactory; public JpaUtil(){ entityManagerFactory=Persistence.createEntityManagerFactory("yourPersistenceUnitName"); } public void persist(Object entity){ EntityManager em=entityManagerFactory.createEntityManager(); try{ em.getTransaction().begin(); em.persist(entity); em.getTransaction().commit(); }finally{ if(em.isOpen()){ em.close(); } } } public void update(Object entity){ EntityManager em=entityManagerFactory.createEntityManager(); try{ em.getTransaction().begin(); em.merge(entity); em.getTransaction().commit(); }finally{ if(em.isOpen()){ em.close(); } } } public void delete(Object entity){ EntityManager em=entityManagerFactory.createEntityManager(); try{ em.getTransaction().begin(); em.remove(entity); em.getTransaction().commit(); }finally{ if(em.isOpen()){ em.close(); } } } public <T> T find(Class<T> entity, Object param){ EntityManager em=entityManagerFactory.createEntityManager(); try{ return em.find(entity,param); }finally{ if(em.isOpen()){ em.close(); } } } @SuppressWarnings("unchecked") public <T> T getSingleResult(String sql,Class<T> clazz){ EntityManager em=entityManagerFactory.createEntityManager(); try{ Query query=em.createNativeQuery(sql,clazz); return (T)query.getSingleResult(); }finally{ if(em.isOpen()){ em.close(); } } } @SuppressWarnings("unchecked") public <T> T getSingleResult(String sql,List<Object> params,Class<T> clazz){ EntityManager em=entityManagerFactory.createEntityManager(); try{ Query query=em.createNativeQuery(sql,clazz); if(params!=null){ for(int i=0; i<params.size();i++){ query.setParameter(i+1, params.get(i)); } } return (T)query.getSingleResult(); }finally{ if(em.isOpen()){ em.close(); } } } @SuppressWarnings("unchecked") public <T> List<T> getResultList(String sql,Class<T> clazz){ EntityManager em=entityManagerFactory.createEntityManager(); try{ Query query=em.createNativeQuery(sql,clazz); return (List<T>)query.getResultList(); }finally{ if(em.isOpen()){ em.close(); } } } @SuppressWarnings("unchecked") public <T> List<T> getResultList(String sql,List<Object> params,Class<T> clazz){ EntityManager em=entityManagerFactory.createEntityManager(); try{ Query query=em.createNativeQuery(sql,clazz); if(params!=null){ for(int i=0; i<params.size();i++){ query.setParameter(i+1, params.get(i)); } } return (List<T>)query.getResultList(); }finally{ if(em.isOpen()){ em.close(); } } } }
I hope the above information was helpful. Be free to think how you may utilize the similar approach in your projects. ;)