不会导致“不会通过刷新处理集合"的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未由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屋!