Hi,
I read the book Java Persistence, and i implemented my HibernateSessionRequestFilter with JPA (EntityManager). My question is: I made correctly? The samples in the book use Hibernate Core.
( ps: the code works, but, i could have problems of performance in production environment? )
My HinernateUtil (JPA version):
-----------------------------------------------------------
package com.modeloweb1.util;
import javax.persistence.*;
/**
*
* @author jacob
*/
public class HibernateUtil {
private static EntityManagerFactory emf;
private static EntityManager em;
private static EntityTransaction tx;
/**
* Creates a new instance of UtilEntityManagerFactory
*/
public UtilEntityManagerFactory() {
}
public static EntityManager getEntityManager() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("modeloWeb1PU");
}
if (em == null) {
em = emf.createEntityManager();
}
return em;
}
}
-----------------------------------------------------------
My HibernateSessionRequestFilter (JPA version):
-----------------------------------------------------------
package com.modeloweb1.util;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.persistence.*;
/**
*
* @author jacob
*/
public class HibernateSessionRequestFilter implements Filter {
private EntityManager em;
private EntityTransaction tx;
/** Creates a new instance of HibernateSessionRequestFilter */
public HibernateSessionRequestFilter() {
}
public void init(FilterConfig filterConfig) throws ServletException {
em = HibernateUtil.getEntityManager();
tx = em.getTransaction();
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
tx.begin();
chain.doFilter(request, response);
tx.commit();
}
public void destroy() {
}
}
My DepartmentDAO (JPA version - resumed):
-----------------------------------------------------------
package com.modeloweb1.dao;
import javax.persistence.*;
import com.modeloweb1.model.*;
import java.util.List;
import com.modeloweb1.util.*;
/**
*
* @author jacob
*/
public class DepartmentDAO extends AbstractDAO {
/** Creates a new instance of DepartmentDAO */
public DepartmentDAO() {
em = UtilEntityManagerFactory.getEntityManager();
}
public List<Department> pesquisar() {
List<Department> departments;
departments = em.createNamedQuery("Department.getAllDepartment").getResultList();
return departments;
}
}
|