-->
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 fetching across sessions
PostPosted: Fri Apr 15, 2011 5:22 pm 
Newbie

Joined: Fri Apr 15, 2011 5:15 pm
Posts: 2
I am new to Hibernate/Spring and I have been running into alot of hibernate problems as I am reworking some code. Specifically with LazyInitializiationExpeception errors. Basically, we have a query layer that initializes query objects and them passes them back to all different parts of the infrastructure. The problem that I keep running into is that after the query objects are passed back I get "LazyInitializiationExpeception - could not initialize proxy - the owning session was closed" errors. From reading online I understand that this is occurring because the the lazy fetches are out of scope of the session. My question is for large infrastructures what is the common design approach to having sessions alive, or is there a way to address lazy fetching (besides switching to eager fetching) between different sessions?

Also what precisely are the scoping requirements for when an object can still reach the session?

thanks


Top
 Profile  
 
 Post subject: Re: Lazy fetching across sessions
PostPosted: Sat Apr 16, 2011 2:32 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
For a servlet layer type app, see the Open Session in View pattern, often done with Spring. Outside of that, you often know the problem spots and can call something like this on the Collections contents as used:

session.lock(obj, LockMode.NONE);
Hibernate.initialize(obj)

Another alternative is to skip lazy altogether on your Collections and load everything "eagerly" , ie "lazy=false" .


Top
 Profile  
 
 Post subject: Re: Lazy fetching across sessions
PostPosted: Sun Apr 17, 2011 9:59 am 
Newbie

Joined: Fri Apr 15, 2011 5:15 pm
Posts: 2
Except for debugging purposes I don't want to just switch over to eager fetching, because I have nested data structures that I don't want it to eager fetch. I will check out the Open Session in View pattern. Thanks.

iksrazal: Does your session.lock example basically perform an eager fetch for a specific retrieval?

My hope is that there is a design pattern for large infrastructures as to where sessions and DAOs should get created and/or kept open?


Top
 Profile  
 
 Post subject: Re: Lazy fetching across sessions
PostPosted: Sun Apr 17, 2011 12:43 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
bojengle wrote:
iksrazal: Does your session.lock example basically perform an eager fetch for a specific retrieval?


No, but the question isn't really correct AFAIK. All it does is re-attach the Object to the hibernate session, so further processing can continue. Where lazy errors typically happen in my experience is from the Set etc side of a one-to-many, on the individual Objects. Eager will load the whole collection as its queried.


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.