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.  [ 2 posts ] 
Author Message
 Post subject: strange lazy load error after session.clear
PostPosted: Mon Jul 24, 2006 1:58 pm 
Newbie

Joined: Mon Jul 24, 2006 1:35 pm
Posts: 10
Hi,

Here's the problem I'm facing: I have two tables - PARENT and CHILD, and I created Hibernate classes for those: Parent and Child. Parent is in one-to-many relationship with Child:

Parent { Set<Child> children;}
Child{}

Here's the code that throws "org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: Parent.children, no session or session was closed":

parent=(Parent)session.load(parentId);
session.clear();
parent.getChildren().size(); <<< ERROR!

On the other hand, this code works fine:

parent=(Parent)session.load(parentId);
parent.getChildren().size();
session.clear();
parent.getChildren().size(); <<< WORKS FINE!

I don't understand what's going on. Why accessing child collection makes a difference if session is cleared and all data should be erased anyway? I would imagine this code should throw an error in both cases.
Can someone explain why this is happening?
Thanks,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 3:22 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Your collection is lazy, so the Set of children isn't initialized and read from the db when you call that first load command. When you call parent.getChildren().size(); is when it finally loads that collection and reads the children from the database.

session.clear(), as noted by the apis

Quote:
Completely clear the session. Evict all loaded instances...


which removes any references between that object and the open session. Which means you cannot load an object's children after you clear the session.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.