No persister for: Castle.Proxies.<EntityName>Proxy and lazy="true" in NHibernate?
Asked Answered
K

3

7

I am trying to use lazy loading for a property of one of my entities

The property mapping is something like this:

<property name="Foobar" type="AnsiString" column="FOOBAR" lazy="true"/>

However when I am tring to save an instance of this entities (using Linq), it throws a DatabaseQueryException with the following inner exception:

NHibernate.MappingException: No persister for: Castle.Proxies.FooEntityProxy"

And when I remove the lazy="true" item, the exception doesn't get thrown anymore. What is the problem with using lazy="true" and how to fix this?

Kos answered 22/6, 2011 at 2:58 Comment(3)
What happened to that property in your session, Has it been loaded or not? Have you modified it in the session?Parenteau
@Pedro Assembly Version is 3.1Kos
is FooEntity subclass, maybe that is the problem....Grafting
O
4

I know this is a late answer, but I had this same problem earlier. I was using a custom interceptor to create the proxies, and so I found that the issue was that I hadn't overridden the "GetEntityName" method. Analyzing the proxy within the GetEntityName method, and returning the proper class name did the trick.

In my case, I had a simple extension method to unproxy my objects, called "UnProxy", so my whole implementation of this method looked like this:

public override string GetEntityName(object entity)
{
    if (entity == null)
        return base.GetEntityName(entity);
    return entity.UnProxy().GetType().FullName;
}
Oesophagus answered 6/10, 2011 at 15:19 Comment(0)
R
3

Are you sure you are using NHibernate 3? I think only this version supports scalar properties lazy loading!

update

Not sure if it can help you but try to look here:

NHibernate lazy loading property - what does build-time bytecode instrumentation mean?

or here:

NHibernate lazy properties behavior?

Roentgenograph answered 24/6, 2011 at 7:54 Comment(0)
G
2

If you mark a property as lazy, it must be a virtual automatic property (with no body like public virtual MyType Baz { get; set; }). If you attempt to access the underlying field value, instead of going through the property, you will circumvent the lazy loading of the property, and may get unexpected results.

Garfieldgarfinkel answered 24/6, 2011 at 7:57 Comment(2)
what is a virtual automatic property?Kos
A virtual property with no body, e.g. public virtual SomeType Foo { get; set; }Bide

© 2022 - 2024 — McMap. All rights reserved.