-->
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.  [ 12 posts ] 
Author Message
 Post subject: Avoiding LazyInitializationException
PostPosted: Sun Aug 01, 2004 8:59 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
So I am working with the Weblog example from the docs. Let's say I want a blog without its blog items. If you set lazy=true in Blog.hbm.xml, that avoids the query for looking up the BlogItems. So my code looks like this:

Session session = sesFac.openSession();
session.load(blog,blog.getId());
session.close();
return blog;

So then elsewhere in my application when I try to use this Blog instance, if I call and methods in the items property, I get a LazyInitializationException. I assume this is because hibernate is setting the items property of the Blog to an implementation of List that does that. If I change my code to do this:

Session session = sesFac.openSession();
session.load(blog,blog.getId());
blog.setItems(new ArrayList());
session.close();
return blog;

Blog.items works like normal, for example if I call size() it returns 0 instead of throwing an exception. Is there better way in hibernate to accomplish this? I guess what I want is "no initialization".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 6:22 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Lazy initialized objects need opened Hibernate session, to initialize all dependent objects on demand.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 9:06 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
For many web applications, the best approach seems to have the Hibernate session & transaction handling done by a ServletFilter.

This way, your session if opened until the HTTP request is actually completed. Same for the transaction management.

Use this together with the ThreadLocal pattern to store a reference to the Hibernate session your filter has opened.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 7:39 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
What if its inside an EJB container with a fat-client? I'm currently running a test with this configuration and I need to close my session (in a SLSB) after performing a query and load it to some course grain (non-hibernate) class. This will raise a lazy exception.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 7:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Then you query and fetch all objects needed before closing the Session. Random lazy loading is not what you want.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 7:53 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It must be better to use hibernate directly in fat client without any EJB,
SLSB has no value in this case any way and this RMI wrapper causes more problems than doe's something usefull.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:20 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
baliukas wrote:
It must be better to use hibernate directly in fat client without any EJB,
SLSB has no value in this case any way and this RMI wrapper causes more problems than doe's something usefull.

Oops, and what about container managed transactions (I mean all those required, requires-new, mandatory) and declarative security (even if you don't need that "static" security, container authentication is very helpful). To use or not to use SLSB depends on a lot requirements, baliukas, I think you are not too correct.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:32 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
SLSB do not maintain state and there is no transaction and security context propagation. SLSB is nothing more than trivial RMI wrapper (a few lines of custom and more portable code)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:35 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is not true, there is of course a transaction and security context. Juzoas, you tend to simplify a lot in the last couple of weeks and tell everyone that they should just throw away all they have and start with some better approach. While you are sometimes right, it doesn't really help anyone. Maybe you should go on vacation for some days? :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:42 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, there is context per method call, is it usefull ?
BTW that is vacation ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:01 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
One of ways is to run container on client, if you must to use EJB use local interface without LazyInitializationException and TDO stuff, Spring is a good alternatyve if you do not like home made solutions too, it just works better.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 11:09 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:41 am
Posts: 20
christian wrote:
Then you query and fetch all objects needed before closing the Session. Random lazy loading is not what you want.


I tried using Jakarta Collections or BeanUtils copyProperties, it works if its not in an EJB container since its written in EJB spec that reflections dont work (?). I could do a copy attribute per attribute but its a little tiring.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.