You can't lazy load after a session has been closed.

 

"Is there a possibility to access the eager loaded references after the
session is closed" 

The answer to this is NO, eager loading occurs the same time as loading the
parent.

 

 

 

From: [email protected] [mailto:[email protected]] On Behalf
Of hamburger
Sent: 30 November 2012 09:47
To: [email protected]
Subject: [nhusers] Problem with Lazy Loading and Eager Fetching

 

Hi,

 

i've got the classes A and B like that:

public class A

{

  public virtual int ID{get;set;}

  public B ReferenceToB{get;set;}

}

 

public class B

{

  public virtual int ID{get;set;}

  public A ReferenceToA{get;set;}

}

 

My mappings:

 

<class table="TABLE_A" name="A">

    <id name="ID" column="ID">

      <generator class="assigned" />

    </id>

    <many-to-one name="B" column="B_ID" lazy="no-proxy" />

</class>

 

 

<class table="TABLE_B" name="B">

    <id name="ID" column="ID">

      <generator class="assigned" />

    </id>

    <many-to-one name="A" column="A_ID" lazy="no-proxy" />

</class>

 

Now I built the following query:

var query = QueryOver.Of<A>()

                .Fetch(a => a.B).Eager;

 

 

Now to the problem. When I execute the query in my DataAccess-Layer with
query.GetExecutableQueryOver(session) the generated SQL says that the
reference to B was loaded.

But if I want to access the Property B a
NHibernate.LazyInitializationException will be thrown. I think it has
something to do with my session. At the time I want access the property B my
session is already closed. When I debug the query result and look at the
generated proxy-class I found a non-public field __interceptor which holds
the TargetInstance. In this TargetInstance my eager-Loaded Property B is
set.

 

Is there a possibility to access the eager loaded references after the
session is closed? I want to do this because I've got a data structure with
several references to different classes. I have to work on this data
structure in different tasks on different ways. Sometimes I do not need some
references as they are not interesting me for the current task. But in an
other task I need these references. Instead of lazy load the references when
I access them I want to load all needed references at one time because I
already know that I will need them for my task. My idea was to enable
lazy-loading by default and fetch all needed references in my query.

 

Sorry for my bad english but I hope everybody understand my problem and
someone can help? Perhaps I make some general mistakes with using lazy
loading? It's logical that I can not access to lazy loaded properties when
the session is closed but I thought i can "switch off" the lazy loading with
fetching eagerly and then have access to the properties after closing the
session.

 

Thank u!

 

-- 
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/3n6lueUqq6sJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to