-->
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.  [ 3 posts ] 
Author Message
 Post subject: LazyInitializationException in ASP.NET app with NHibernate
PostPosted: Sun Nov 28, 2010 2:04 am 
Newbie

Joined: Sun Nov 28, 2010 2:01 am
Posts: 3
Using NHibernate with ASP.NET 4.

I have had no issues retrieving objects, but I've just got to a point with nested objects that I can't figure out.

I am using lazy=true and when accessing a lazy-load collection I get the message:

Initializing[type#3]-failed to lazily initialize a collection of role: [type], no session or session was closed

Even if I call SessionFactory.OpenSession() immediately prior to the object being accessed, it makes no difference. I have also tried accessing the collection in a _using ISession_ block to no luck.

I do have current_session_context_class = web set in web.config, and I am using CurrentSessionContext.Bind on BeginRequest in Global.asax.

Can anyone offer some advice?

This is ASP.NET **NOT** ASP.NET MVC


Top
 Profile  
 
 Post subject: Re: LazyInitializationException in ASP.NET app with NHibernate
PostPosted: Wed Dec 01, 2010 8:53 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
Follow the OSIV (Open Session In View) term. The trick is that you open session at the beginning of the request and close it when request ends. This way you always have the session open and lazy things can get initialized.

On the other thing - it is not enough to just open session. You must bind the detatched object to the particular session to initialize lazy things.

Code:
session.Lock(obj, LockMode.None);

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject: Re: LazyInitializationException in ASP.NET app with NHibernate
PostPosted: Fri Dec 03, 2010 9:03 am 
Newbie

Joined: Sun Nov 28, 2010 2:01 am
Posts: 3
Cheers mate - worked a treat.


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