Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 911
Louis Rhys Avatar asked Jun 22 '11 02:06

Louis Rhys


3 Answers

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;
}
like image 159
David Morton Avatar answered Nov 06 '22 20:11

David Morton


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?

like image 24
danyolgiax Avatar answered Nov 06 '22 20:11

danyolgiax


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.

like image 36
Tomasz Jaskuλa Avatar answered Nov 06 '22 20:11

Tomasz Jaskuλa