NHibernate 拦截器审核插入的对象 ID

发布于 2024-07-20 02:53:53 字数 665 浏览 5 评论 0原文

我正在使用 NHibernate 拦截器来记录有关我的各个实体的更新/插入/删除的信息。

记录的信息中包括实体类型和所修改实体的唯一 ID。 唯一的 Id 在 NHibernate 映射文件中标记为

明显的问题是,当使用 IInterceptor.OnSave() 记录插入操作时,实体的 Id 尚未分配。

在记录审计信息之前如何获取插入实体的Id?

(我已经研究了 NHibernate Listeners PostSave 事件,但无法让它们与正在使用的 Spring.net 配置一起工作,所以如果可能的话,我想坚持使用拦截器)

代码:

    // object id parameter is null...
    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {            
        AddAuditItem(entity, INSERT);
        return false;            
    }

I am using NHibernate interceptors to log information about Updates/Inserts/Deletes to my various entities.

Included in the information logged is the Entity Type and the Unique Id of the entity modified. The unique Id is marked as a <generator class="identity"> in the NHibernate mapping file.

The obvious problem is when logging an Insert operation using IInterceptor.OnSave() the Id of the entity has not yet been assigned.

How can I obtain the Id of the inserted entity before logging the audit information?

(I have looked into NHibernate Listeners PostSave event but can't get them working with the Spring.net configuration being used, so I would like to stick with interceptors if at all possible)

Code:

    // object id parameter is null...
    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {            
        AddAuditItem(entity, INSERT);
        return false;            
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2024-07-27 02:53:54

我通过向拦截器类添加一个列表来解决这个问题,该列表在 OnSave 实现期间填充了对象。

PostFlush 实现中,列表被迭代,每个元素都被审核为插入。 此列表中的对象已在 PostFlush() 中持久化,因此已生成 ID。

这似乎工作正常,但如果指出任何潜在的陷阱,我将不胜感激:-)

public class AuditInterceptor : EmptyInterceptor
{       
    // To hold inserted items to be audited after insert has been flushed
    private IList<object> insertItems = new List<object>();

    public override void PostFlush(System.Collections.ICollection entities)
    {            
        foreach (var entity in insertItems)
        {
            AddAuditItem(entity, INSERT);
        }
        insertItems.Clear();

        base.PostFlush(entities);
    }

    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {            
        var auditable = entity as IAuditable;
        if (auditable != null) 
            insertItems.Add(entity);

        return false;            
    }
}

I've worked around this problem by adding a list to my interceptor class which is populated with objects during the OnSave implementation.

In the PostFlush implementation the list is iterated over and each element is audited as an insert. The objects in this list have been persisted in PostFlush() and thus have generated IDs.

This seems to work OK but I'd be grateful if any potential pitfalls were pointed out :-)

public class AuditInterceptor : EmptyInterceptor
{       
    // To hold inserted items to be audited after insert has been flushed
    private IList<object> insertItems = new List<object>();

    public override void PostFlush(System.Collections.ICollection entities)
    {            
        foreach (var entity in insertItems)
        {
            AddAuditItem(entity, INSERT);
        }
        insertItems.Clear();

        base.PostFlush(entities);
    }

    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {            
        var auditable = entity as IAuditable;
        if (auditable != null) 
            insertItems.Add(entity);

        return false;            
    }
}
北渚 2024-07-27 02:53:54

尝试 OnFlushDirty 方法..或者也许 PostFlush

编辑:另外,你可以发布你的代码吗? 您没有将 Id 作为 OnSave 的参数吗?

try the OnFlushDirty method.. or maybe PostFlush

edit: also, can you post your code? Don't you get the Id as a parameter to OnSave?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文