-->
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.  [ 7 posts ] 
Author Message
 Post subject: LazyInitializationException on *second* access
PostPosted: Thu Sep 03, 2009 9:19 pm 
Newbie

Joined: Wed Oct 01, 2008 2:27 pm
Posts: 18
After accessing a @ManyToOne(fetch = FetchType.LAZY) property inside a session, shouldn't it be possible to access the same property again after the session has been closed?

Hibernate (3.3.1.GA) appears to think not:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at test.Foo_$$_javassist_8.toString(Foo_$$_javassist_8.java)
...

Is this a bug, feature, or perhaps I'm doing something wrong?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 12:25 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I guess it depends on which side of the relation the original get request was for.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 2:14 am 
Newbie

Joined: Wed Oct 01, 2008 2:27 pm
Posts: 18
Cameron McKenzie wrote:
I guess it depends on which side of the relation the original get request was for.


Here's as example:

foo.getLazyBar().toString()

This call succeeds when I call it the first time (within a session). The lazyBar is loaded I get the toString(). Then I close the session and run the exact same statement again -- but now it fails with a LazyInitializationException. This is surprising as I'd expect FetchType.LAZY on an @ManyToOne to behave like it does on lazy collections (i.e. first access initializes the collection, thereafter it can be used outside of the original session).

So the question is: Is this supposed to work?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 7:43 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, your understanding is correct. If loaded, the relationship stays there.

There must be something peculiar about our code. I'm wondering if you somehow put the object out of scope in your second iteration.

Maybe you can show us more code?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 1:51 pm 
Newbie

Joined: Wed Oct 01, 2008 2:27 pm
Posts: 18
Cameron McKenzie wrote:
Well, your understanding is correct. If loaded, the relationship stays there.

There must be something peculiar about our code. I'm wondering if you somehow put the object out of scope in your second iteration.

Maybe you can show us more code?


Thanks for confirming that this is supposed to work.

How can the object go out of scope while I'm still referencing it (unless javassist is using weak/soft references)?

I'll try to come up with a simple test case...


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 5:01 pm 
Newbie

Joined: Wed Oct 01, 2008 2:27 pm
Posts: 18
Turns out the initialized object was sneakily being replaced with an uninitialized one -- my bad. Case closed :-)


Top
 Profile  
 
 Post subject: Re: LazyInitializationException on *second* access
PostPosted: Fri Sep 04, 2009 7:18 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Heh...Thanks for the follow up. Sometimes the most difficult problems to solve have the simplest solutions.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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