-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Implementing HibernateSessionRequestFilter w/ EntityManager
PostPosted: Mon Jun 25, 2007 10:22 pm 
Newbie

Joined: Mon Jun 25, 2007 9:55 pm
Posts: 1
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;
}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.