-->
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.  [ 4 posts ] 
Author Message
 Post subject: EJB3 persistence (using hibernate) in a Tomcat webapp
PostPosted: Wed Aug 16, 2006 6:24 am 
Newbie

Joined: Wed Aug 16, 2006 6:20 am
Posts: 2
(When you only want to use EJB3 persistence, and not the whole EJB3 stack).

In the document "Using Hibernate with Tomcat" (http://www.hibernate.org/114.html), it is suggested that you create a ServletContextListener - like the one below - where the listener initializes and closes Hibernate on deployment and undeployment of your webapp.

Code:
public class HibernateListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        HibernateUtil.getSessionFactory(); // Just call the static initializer of that class   
    }

    public void contextDestroyed(ServletContextEvent event) {
        HibernateUtil.getSessionFactory().close(); // Free all resources
    }
}


My question is: If I want to use Hibernate as an EJB3 persistence engine in a Webapp running in Tomcat, do I make a similar ServletContextListener like this:


Code:
public class EntityManagerListener implements ServletContextListener {
   
    public void contextInitialized(ServletContextEvent event) {
        EntityManagerUtil.getEntityManagerFactory(); // Just call the static initializer of that class   
    }

    public void contextDestroyed(ServletContextEvent event) {
        EntityManagerUtil.getEntityManagerFactory().close(); // Free all resources
    }

}

Code:
public class EntityManagerUtil {

    private static EntityManagerFactory emf;
    public static final ThreadLocal<EntityManager> entitymanager = new ThreadLocal<EntityManager>();
   
    public static EntityManagerFactory getEntityManagerFactory() {
        if (emf == null) {
            // Create the EntityManagerFactory
            emf = Persistence.createEntityManagerFactory("example");
        }
       
        return emf;
    }
   

    public static EntityManager getEntityManager() {
        EntityManager em = entitymanager.get();

        // Create a new EntityManager
        if (em == null) {
            em = emf.createEntityManager();
            entitymanager.set(em);
        }
        return em;
    }

    public static void closeEntityManager() {
        EntityManager em = entitymanager.get();
        entitymanager.set(null);
        if (em != null) em.close();
    }

}


And then in my webapp use the following code:

Code:
public Person findByName(String name) {
    EntityManager em = EntityManagerUtil.getEntityManager();
    Query q = em.createQuery("select person from Person as person where name=:param");
    q.setParameter("param", name);
    Person p = (Person) q.getSingleResult();
    em.close();
    return p;
}


Would that be the right way to do it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 6:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes, but using JBoss Embeddable EJB3 would be much easier and powerful. I don't understand why people use a Lexus when you provide them a Mercedes for the same price ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 19, 2006 6:08 am 
Newbie

Joined: Wed Aug 16, 2006 6:20 am
Posts: 2
emmanuel wrote:
yes, but using JBoss Embeddable EJB3 would be much easier and powerful...

Properly true (I don't know JBoss Emb EJB3), but if all I want is EJB3 persistence instead of plain Hibernate, then this should be enough.


Top
 Profile  
 
 Post subject: JPA and ThreadLocal EntityManager
PostPosted: Wed Jan 31, 2007 11:07 am 
Newbie

Joined: Wed Jan 31, 2007 10:53 am
Posts: 3
I followed the code in this thread and implemented the Listener and the ThreadLocal entity manager.

I am using Jetspeed 2.0 with portlets and am having a hard time determining where to start and end a transaction and how to detach an object (Filters do not work in portlets by the way).

My current methods aren't working. I have a screen that lets the user manipulate a bean, removing items from a collection. Each time they click "Remove" on a line item I remove the actual item from the collection and then if they click Save then I call merge() on the ThreadLocal obtained EntityManager. But if they cancel, I just want to reload it from the database or discard the object.

I can call EntityManager refresh() but because my EntityManager is ThreadLocal, a lot of times it does not refresh because the EntityManager I loaded the entity from is different than the EntityManager I get when they try to Cancel their changes.

If I change it so that I close the EntityManager after the find then I have a problem when I retrieve a list of these objects. The collection ends up being a LAZY load and when the JSP part of the portlet tries to display something from the collection, I get the exception because the session is closed.

Do I need to make everything load EAGER and just close the EntityManager or call clear() on the EntityManager after each load/find?


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.