-->
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: Lazy Initialization Exceptions ... What am I missing here:
PostPosted: Sun Apr 24, 2005 6:07 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
Hibernate 3.0
MySQL

I have the following mind numbingly simple code:
Code:
HibHelper.beginTransaction();
      Session s = HibHelper.getSession();
      Criteria c2 = s.createCriteria("core.User");      
      List l = c2.list();      
      File f2 = new File("c:/temp/encode.xml");
      try {
         XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
               new FileOutputStream(f2)));
         for (int x = 0; x < l.size(); x++) {
            Object o = l.get(x);            
            encoder.writeObject(o);
         }
         encoder.close();
      } catch (Exception e) {
         Log.error(e);
      }


The User Object has within it a collection of Roles.

When I try to run the above code though, I get:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection (core.User.roles) - no session or session was closed

The session though wasn't cloed. In fact it's a thread-local session so I never close it at all. So what am I missing here? Why am I seeing this error and how can I prevent it?


Top
 Profile  
 
 Post subject: It appears this may be a known limitation
PostPosted: Sun Apr 24, 2005 6:18 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
http://opensource.atlassian.com/project ... key=HB-320


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 3:09 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
I guess you are trying to acces the role from user after the session is closed.
Can you post the entire thing, the xml's and the code and exception trace

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 11:29 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
yvijaykumar wrote:
I guess you are trying to acces the role from user after the session is closed.
Can you post the entire thing, the xml's and the code and exception trace

thanks


It turns out that wasn't the problem. According to the article I quoted above, the built in java xml serializer works by first cloning the object graph (to see the relationships), and then serializing the pieces. Evidently its clone isn't perfect though and it doesn't properly clone the Hibernate collections so they lose touch with the session. So basically you can't use the java xml serializer with a lazily initialized hibernate object graph.

My solution was just to roll my own xml serializer that doesn't have the clone step; works like a champ.


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.