NHibernate.HibernateException: instance not of expected entity type

NHibernate.TransactionException: An error occured when trying to dispose the transaction —> NHibernate.HibernateException: instance not of expected entity type: Model.OnState is not a: Model.UnitState
at NHibernate.Persister.Entity.AbstractEntityPersister.GetSubclassEntityPersister(Object instance, ISessionFactoryImplementor factory, EntityMode entityMode)
at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
at NHibernate.Engine.ForeignKeys.IsTransient(String entityName, Object entity, Nullable`1 assumed, ISessionImplementor session)
at NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity, String entityName, EntityEntry entry, ISessionImplementor source)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj)
at NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything)
at NHibernate.Event.Default.AbstractFlushingEventListener.CascadeOnFlush(IEventSource session, IEntityPersister persister, Object key, Object anything)
at NHibernate.Event.Default.AbstractFlushingEventListener.PrepareEntityFlushes(IEventSource session)
at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at Castle.ActiveRecord.TransactionScope.PerformDisposal(ICollection`1 sessions)
— End of inner exception stack trace —
at Castle.ActiveRecord.TransactionScope.PerformDisposal(ICollection`1 sessions)
at Castle.Facilities.ActiveRecordIntegration.TransactionScopeResourceAdapter.Commit()
at Castle.Services.Transaction.AbstractTransaction.Commit()

    [ActiveRecord(
        DiscriminatorColumn = "type",
        DiscriminatorType = "String",
        DiscriminatorValue = "UnitState")]
    public abstract class UnitState {
 
        [PrimaryKey]
        public long Id { get; set; }
 
        //...
    }
 
    [ActiveRecord(DiscriminatorValue = "OffState")]
    public class OffState: UnitState {
 
        private OffState() {
        }
 
        public OffState(Unit unit)
            : base(unit) {
        }
 
        //...
    }
    [ActiveRecord(DiscriminatorValue = "OnState")]
    class OnState: UnitState {
 
        private OnState() {
        }
 
        internal OnState(Unit unit)
            : base(unit) {
        }
 
        //...
    }

Took me quite a while to figure this one out but the issue here was that OnState was not declared public.

No comments yet.

Leave a Reply