我在一个单一的遗留数据库之上有一个 NHibernate 映射,自从我们在生产中推出以来,我们每天大约 4-8 次从 NHibernate 收到错误,每天处理大约 60 000 个事件,所以错误频率约为 0.006% - 0.013%)。
据我和我的同事能够确定,与成功的事件/消息相比,这些失败的事件/消息没有什么不同。
我们得到的错误是
NHibernate.AssertionFailure: null identifier
at NHibernate.Engine.EntityKey..ctor(Object identifier, String rootEntityName, String entityName, IType identifierType, Boolean batchLoadable, ISessionFactoryImplementor factory, EntityMode entityMode)
at NHibernate.Engine.EntityKey..ctor(Object id, IEntityPersister persister, EntityMode entityMode)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
但是标识符列是一个值类型(System.Int32),使用Native生成器。怎么可能是空的?
public class MyItem {
public int MyItemId { get; set; }
}
public class MyItemMap : ClassMapping<MyItem>
{
public MyItemMap ()
{
Table("MyItemTable");
Id(m => m.MyItemId, x =>
{
x.Column("MyItem_ID");
x.Generator(Generators.Native);
x.UnsavedValue(default(int));
});
}
}
所以,我的问题是真的;a) 当标识符是值类型(即不可为空)时,为什么 NHibernate 会抱怨空标识符?b) 我能做些什么吗?