Reputation: 420
I am working with single session, my application contains different modules and object fetched from session are retained. As I call to Session.Clear from any module to refresh session or entity. I am getting the exception "failed to lazily initialize - no session or session was closed" on get of child entities in any other modules.
[LazyInitializationException] failed to lazily initialize a collection of role: Application.Modules.Repository.childRepository, no session or session was closed
This how I am creating a Session factory and Session :
var createdSessionFactory = Fluently.Configure(configuration)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<DMProgram>())
.ExposeConfiguration(c => c.SetProperty("command_timeout", "500"))
.BuildSessionFactory();
ISession Session = NHibernateConfiguration.SessionFactory.OpenSession();
Upvotes: 2
Views: 9376
Reputation:
Case : When we get a object(A) from session. that object has some collection(B) which is lazy load. you close or clear the session. now you are trying to get the collection(B). sure you get this error.
Cause : Because session does not know parent object(A).
Solution : You need to persist session or you can eager load collection(B).
Upvotes: 3