Skip to content

The session.Load(obj, id) overload can't handle proxies #2727

Closed
@cremor

Description

@cremor

If you pass a proxy to the ISession.Load(object obj, object id) overload the following exception is thrown:

NHibernate.MappingException: No persister for: XyzProxy // or XyzProxyForFieldInterceptor
   at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
   at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
   at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
   at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
   at NHibernate.Impl.SessionImpl.Load(Object obj, Object id)

Both "normal" lazy load proxies and proxies for lazy properties are affected.

I took a quick look a the code and it seems like the problem is that SessionImpl.GetEntityPersister() uses SessionImpl.GuessEntityName() which can't handle the case that the passed entity is a proxy. It will just return "XyzProxy" or "XyzProxyForFieldInterceptor" in that case.
I think SessionImpl.GetEntityPersister() should use SessionImpl.BestGuessEntityName() instead. That method seems to support proxies.

But even if this is fixed the next problem might be at

@event.EntityClassName = @event.InstanceToLoad.GetType().FullName;

This would again set "XyzProxy" or "XyzProxyForFieldInterceptor" to EntityClassName.

It seems like ISession.Refresh() has a similar problem, but with that I could only reproduce it if the passed entity is not contained in the session and if it has lazy properties. Since I don't use Refresh() in my application I didn't look at this further.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions