I am using a disconnected type design in my app whereby I get my objects from a repository which loads the objects with the required object graph , closes the session, and returns the objects to the client.
I get a
"Failed to lazily initialize a collection - no session" even when I have explicitly told NHibernate to do a Join using the following code:
Code:
ICriteria criteria = localSession.CreateCriteria(typeof (Document));
criteria.Add(Expression.Eq("DocumentID", documentID));
criteria.SetFetchMode("Attributes", FetchMode.Join);
criteria.SetFetchMode("Rfi", FetchMode.Join);
criteria.SetFetchMode("Rfi.SubProject", FetchMode.Join);
criteria.SetFetchMode("Rfi.SubProject.AttributeDefinitions", FetchMode.Join);
criteria.SetFetchMode("Rfi.SubProject.DocumentNamingFormats", FetchMode.Join);
criteria.SetFetchMode("Rfi.SubProject.AllowedOrganisationDisciplines", FetchMode.Lazy);
criteria.SetFetchMode("Rfi.SubProject.AllowedTransmittalTypeRecipientTransmittalPurposes", FetchMode.Lazy);
criteria.SetFetchMode("Rfi.SubProject.AllowedTransmittalTypes", FetchMode.Lazy);
criteria.SetFetchMode("Rfi.SubProject.DocumentSubTypes", FetchMode.Lazy);
criteria.SetFetchMode("Rfi.SubProject.AllowedDocumentTypeDocumentSubTypes", FetchMode.Lazy);
criteria.SetResultTransformer(CriteriaUtil.DistinctRootEntity);
results = criteria.List();
I even tried the nasty hack of iterating through the collection to ensure the collections are loaded:
Code:
//HACK: To get around the eager loading issue
foreach(object o in doc.Rfi.SubProject.DocumentNamingFormats)
{
//Do nothing
}
foreach(object o in doc.Rfi.SubProject.AttributeDefinitions)
{
//Do nothing
}
foreach(object o in doc.Attributes)
{
//Do nothing
}
Is this a bug or am I doing something unexpected?
The funny thing is there is a workaround that I can use in that if I copy an object into another new variable, this behaviour goes away... again, is this a bug or a feature?