-->
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 Loading
PostPosted: Fri May 08, 2009 11:48 am 
Newbie

Joined: Tue Apr 07, 2009 8:48 am
Posts: 14
Location: Kerala,India
Hi guys,

I am having a problem with the lazy loading of Hibernate. I have set lazy to default value ie: lazy="true" in my HBM files. So whenever i try to retrieve data from the child table, its throwing Lazy Initailization Exception could not initialize proxy - no Session. I tried to load the parent class and initialize the parent object, but the exception remains the same.

My application has Spring so I am using HibernateTemplate. I cannot set LAZY="false" as it leads to poor perfomance and speed.

Please help me in this matter.

Thanks in advance.

_________________
Its fun to LEARN
guru_G


Top
 Profile  
 
 Post subject: Re: Lazy Loading
PostPosted: Fri May 08, 2009 2:10 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
A LazyInitializationException happens when you try to access an object association outside the session:

-- start session --
load objectA
-- end session --
objectA.getB() // Exception

Setting lazy=false is the wrong solution, but you found that out already.

In our app (which uses Spring and Hibernate too) the Session/Transaction Handling is set by @Transactional Annotations on the relevant methods.

My guess is that you load the object in one method, then pass it to another method or class (and thereby leaving the session/transaction) and then try to access the child.

I suggest you read the Spring manual on how to setup this correctly

Rating is welcome

Patrik


Top
 Profile  
 
 Post subject: Re: Lazy Loading
PostPosted: Sat May 09, 2009 4:59 am 
Newbie

Joined: Tue Apr 07, 2009 8:48 am
Posts: 14
Location: Kerala,India
Thanks Patrik.

I am very much new to Hibernate and I know little about Hibernate Annotations. I think its an alternative for HBM files(Correct me if I am wrong). I am defining the relationships between my tables via HBM files.

I will defenitely go through the Spring Manual.

I found an alternative for LazyInitializationException. A said in my previous post, I was loading the entire parent class and initializing the parent object without loading the parent object I needed. Now I am loading the parent object and trying to access the data through that parent object. Its working fine.

Thank you all for looking into the post.

_________________
Its fun to LEARN
guru_G


Top
 Profile  
 
 Post subject: Re: Lazy Loading
PostPosted: Sat May 09, 2009 6:57 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
@Transactional is a Spring annotation, not a Hibernate Annotation

I'm not sure what you mean by "loading the entire parent class etc" but if it works then fine :o)


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.