一个 NHibernate 审计跟踪不会导致“收集未被刷新处理"错误 [英] An NHibernate audit trail that doesn't cause "collection was not processed by flush" errors
问题描述
Ayende 有一篇关于如何为 NHibernate 实现简单审计跟踪的文章(此处) 使用事件处理程序.
Ayende has an article about how to implement a simple audit trail for NHibernate (here) using event handlers.
不幸的是,从评论中可以看出,他的实现导致抛出以下异常:collection xxx was not processing by flush()
Unfortunately, as can be seen in the comments, his implementation causes the following exception to be thrown: collection xxx was not processed by flush()
问题似乎是对脏属性的ToString 隐式调用,如果脏属性也是映射实体,则会导致问题.
The problem appears to be the implicit call to ToString on the dirty properties, which can cause trouble if the dirty property is also a mapped entity.
我已尽最大努力构建一个可行的实现,但没有成功.
I have tried my hardest to build a working implementation but with no luck.
有人知道可行的解决方案吗?
Does anyone know of a working solution?
推荐答案
我能够使用以下解决方法解决相同的问题:在侦听器中当前持久性上下文中的所有集合上将处理的标志设置为 true
I was able to solve the same problem using following workaround: set the processed flag to true on all collections in the current persistence context within the listener
public void OnPostUpdate(PostUpdateEvent postEvent)
{
if (IsAuditable(postEvent.Entity))
{
//skip application specific code
foreach (var collection in postEvent.Session.PersistenceContext.CollectionEntries.Values)
{
var collectionEntry = collection as CollectionEntry;
collectionEntry.IsProcessed = true;
}
//var session = postEvent.Session.GetSession(EntityMode.Poco);
//session.Save(auditTrailEntry);
//session.Flush();
}
}
希望这会有所帮助.
这篇关于一个 NHibernate 审计跟踪不会导致“收集未被刷新处理"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!