Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NHibernate.Envers and ProxyForFieldInterceptor #2913

Closed
NikitaEgorov opened this issue Sep 16, 2021 · 5 comments
Closed

NHibernate.Envers and ProxyForFieldInterceptor #2913

NikitaEgorov opened this issue Sep 16, 2021 · 5 comments

Comments

@NikitaEgorov
Copy link

NikitaEgorov commented Sep 16, 2021

Envers has code that cast object to INHibernateProxy

private static object getIdentifier(ISessionImplementor session, object obj)
{
	return obj == null
		? null
		: (obj is INHibernateProxy objAsProxy
			? objAsProxy.HibernateLazyInitializer.Identifier
			: session.GetEntityPersister(null, obj).GetIdentifier(obj));
}

Now I have some trouble with object saving.

I get some object with lazy reference from DB
And NHibernate set Lazy property as CantonProxyForFieldInterceptor
Next I create new Object and get Canton from DB using session.Query.Where(x=>x.Id=1)
And Nhibernate return CantonProxyForFieldInterceptor.
This thing completely kill Envers :(

@NikitaEgorov
Copy link
Author

Exception from Envers

NHibernate.MappingException: No persister for: CantonProxyForFieldInterceptor
   at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
   at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
   at NHibernate.Envers.Tools.Toolz.getIdentifier(ISessionImplementor session, Object obj)
   at NHibernate.Envers.Tools.Toolz.EntitiesEqual(ISessionImplementor session, Object obj1, Object obj2)
   at NHibernate.Envers.Entities.Mapper.Relation.ToOneIdMapper.MapToMapFromEntity(ISessionImplementor session, IDictionary`2 data, Object newObj, Object oldObj)
   at NHibernate.Envers.Entities.Mapper.MultiPropertyMapper.Map(ISessionImplementor session, IDictionary`2 data, String[] propertyNames, Object[] newState, Object[] oldState)
   at NHibernate.Envers.Synchronization.Work.AddWorkUnit..ctor(ISessionImplementor sessionImplementor, String entityName, AuditConfiguration verCfg, Object id, IEntityPersister entityPersister, Object[] state)
   at NHibernate.Envers.Event.AuditEventListener.OnPostInsert(PostInsertEvent evt)
   at NHibernate.Action.EntityInsertAction.PostInsert()
   at NHibernate.Action.EntityInsertAction.Execute()
   at NHibernate.Engine.ActionQueue.InnerExecute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions[T](List`1 list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()

@bahusoid
Copy link
Member

Looks related to #2727

@bahusoid
Copy link
Member

So a local fix would be using BestGuessEntityName in Envers getIdentifier:

private static object getIdentifier(ISessionImplementor session, object obj)
{
	return obj == null
		? null
		: (obj is INHibernateProxy objAsProxy
			? objAsProxy.HibernateLazyInitializer.Identifier
			: session.GetEntityPersister(session.BestGuessEntityName(obj), obj).GetIdentifier(obj));
}

@RogerKratz
Copy link
Contributor

@bahusoid thanks, fixed (in envers)

@NikitaEgorov
Copy link
Author

Thanks to all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants