-->
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: How can I make lazy loading work?
PostPosted: Thu Dec 29, 2011 1:07 pm 
Newbie

Joined: Wed Jul 27, 2011 10:17 am
Posts: 5
Hello all

I’m using hibernate 3.6 in a JSE 2-tier environment, configured with annotated classes. Lazy loading doesn’t work, I get a SessionIsClosed exception.

I scanned different webpages and forums and I learned that I have to keep the session open in order to make lazy loading work. The problem is that I don’t have any session because I use the JPA pattern with EntityManagers, and the EntityManagerFactory doesn’t know how to return the current (open) EntityManager. I tried some implementations with a static EnitityManager that remains open, but this didn’t work either.

Is there any simple solution to this problem except of switching to J2EE or using the Hibernate SessionManager pattern?

Here is what a standard “select” method looks like:

Code:
   static protected AbstractPersistentId dbSelect(Class<? extends AbstractPersistentId> cls, Integer id)
   {
      EntityManager entityManager = null;
      EntityTransaction transaction = null;

      AbstractPersistentId obj = null;

      try
      {
         entityManager = EntityManagerFactory.createEntityManager();
         transaction = entityManager.getTransaction();
         transaction.begin();

         Logger.debug("Selecting one for class '" + cls.getName() + "', id: " + id);

         obj = entityManager.find(cls, id);

         Logger.info("Selected one for class '" + cls.getName() + "', found: " + obj);

         transaction.commit();
      }
      catch (RuntimeException ex)
      {
         if (transaction != null)      transaction.rollback();
         throw(ex);
      }
      finally
      {
         if (entityManager != null)      entityManager.close();
      }

      return obj;
   }


I would very much appreciate a hint.

Many thanx in advance
:-Denis


Top
 Profile  
 
 Post subject: Re: How can I make lazy loading work?
PostPosted: Fri Dec 30, 2011 3:30 pm 
Newbie

Joined: Wed Nov 10, 2010 4:24 pm
Posts: 6
You may not be able to use lazy initialization with the configuration you're working with. You need to have an open session for this to work. Have you tried just turning it off lazy fetching in the annotations?

Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = false)
@Table(name = "TABLE_T")
public class Example....


Top
 Profile  
 
 Post subject: Re: How can I make lazy loading work?
PostPosted: Sat Dec 31, 2011 6:46 am 
Newbie

Joined: Wed Jul 27, 2011 10:17 am
Posts: 5
Yes, when I switch off lazy initialization, it works fine. But since I build up a tree structure with parents and children linked together, hibernate fetches the WHOLE TREE (with thousands of nodes) when I actually want to get only one node. And that's of course totally ridiculous.

Many thanx for your answer though.

:-Denis

jhuntley wrote:
You may not be able to use lazy initialization with the configuration you're working with. You need to have an open session for this to work. Have you tried just turning it off lazy fetching in the annotations?

Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = false)
@Table(name = "TABLE_T")
public class Example....


Top
 Profile  
 
 Post subject: Re: How can I make lazy loading work?
PostPosted: Fri Jan 06, 2012 5:44 am 
Newbie

Joined: Wed Jul 27, 2011 10:17 am
Posts: 5
OK, I could solve the problem by myself.


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.