Disabling lazy load

714 views
Skip to first unread message

Vicente

unread,
Sep 2, 2009, 6:14:32 PM9/2/09
to Fluent NHibernate
Hello,

I have a problem with lazy load as I seem unable to disable it. I have
a Path entity that has a collection of Segments. Right now the mapping
for the relation is:

this.HasMany(x => x.Segments)
.KeyColumns
.Add(new string[] { "PathId" })
.Cascade
.SaveUpdate()
.Not
.LazyLoad();

To load the entity I call this method:

public TEntity LoadById(TId id)
{
using (var session = this.SessionFactory.OpenSession())
{
return this.LoadById(id, session);
}
}

That calls this other one:

public virtual TEntity LoadById(TId id, ISession session)
{
return session.Load<TEntity>(id);
}

But when I call this:

Console.WriteLine("Number of segments in Path 1 -> {0}",
DAOFactory.PathDAO.LoadById(path1.Id).Segments.Count);

I get the following exception:

Initializing[CTech.Visualization.Data.Entities.ORM.Path#22]-Could not
initialize proxy - no Session.

Any clues on why I'm getting that exception? Regards,

Vicente

Mikael Henriksson

unread,
Sep 3, 2009, 7:30:00 AM9/3/09
to fluent-n...@googlegroups.com
The problem is not lazy loading the problem is the proxy. You need to instantiate nhibernate and build the sessionfactory at the root level of the application. The session should be per user basically (in a web application save it in HttpContext.Current) that way the user get the same session every time and the proxies that nhibernate uses works. See Ayendes post on the subject. http://ayende.com/Blog/archive/2009/08/06/challenge-find-the-bug-fixes.aspx

Vicente

unread,
Sep 3, 2009, 8:48:59 AM9/3/09
to Fluent NHibernate
Hello,

thanks for your answer, but I don't really understand the relation
between my problem and the post you gave me. First, this is a WPF
application and I create the SessionFactory when the app is
initialized, which is later saved in the DAOFactory and from there is
injected in the different DAOs. I have made a derived class from my
GenericDAO (similar to the one in nHibernate in Action) that overloads
the LoadById method to this:

public override Path LoadById(long id, ISession session)
{
Path p = base.LoadById(id, session); // Performs session.Load<Path>
(id);
int a = p.Segments.Count;
return p;
}

And now everything works fine, but is there not any option to force
nHibernate to load that Segments collection from the DB instead of
using a proxy? (I thought that was what marking the relation as
Not.LazyLoad() would do).

Regards,

Vicente

On Sep 3, 1:30 pm, Mikael Henriksson <mik...@zoolutions.se> wrote:
> The problem is not lazy loading the problem is the proxy. You need to
> instantiate nhibernate and build the sessionfactory at the root level of the
> application. The session should be per user basically (in a web application
> save it in HttpContext.Current) that way the user get the same session every
> time and the proxies that nhibernate uses works. See Ayendes post on the
> subject.http://ayende.com/Blog/archive/2009/08/06/challenge-find-the-bug-fixe...
> > Vicente- Hide quoted text -
>
> - Show quoted text -

Mikael Henriksson

unread,
Sep 3, 2009, 9:08:36 AM9/3/09
to fluent-n...@googlegroups.com
Hi,

I am not 100% sure about how to achieve this in NHibernate since I am just a beginner at all this but I believe what you are after is something like EntityMode.Poco. The proxies are not used only for lazy loading they are used for virtually everything. I think you need to redesign some things if you want to use true poco entities and get rid of the proxies.

In new Entity Framework it's :
this.ContextOptions.DeferredLoadingEnabled = true;
this.ContextOptions.ProxyCreationEnabled = true;

Stuart Childs

unread,
Sep 3, 2009, 10:28:51 AM9/3/09
to fluent-n...@googlegroups.com
I think your problem is you've made the classic mistake of confusing
session.Get and session.Load.

session.Load will always return a proxy object. I can't remember if it
will initialize it from the session cache, but if it's not in the
cache it will be an empty object (except for the ID). If you try to
use the object in any way besides getting the ID, it will trigger a
query and if the proxy object is no longer attached to a session, you
app blows up.

session.Get will always return an object (possibly with uninitialized
collections/associations, depends on your mapping and query) or null
if the object doesn't exist.

See: http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx
Reply all
Reply to author
Forward
0 new messages