NHibernate 拦截器审核插入的对象 ID
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过向拦截器类添加一个列表来解决这个问题,该列表在
OnSave
实现期间填充了对象。在
PostFlush
实现中,列表被迭代,每个元素都被审核为插入。 此列表中的对象已在PostFlush()
中持久化,因此已生成 ID。这似乎工作正常,但如果指出任何潜在的陷阱,我将不胜感激:-)
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 inPostFlush()
and thus have generated IDs.This seems to work OK but I'd be grateful if any potential pitfalls were pointed out :-)
尝试
OnFlushDirty
方法..或者也许PostFlush
编辑:另外,你可以发布你的代码吗? 您没有将 Id 作为
OnSave
的参数吗?try the
OnFlushDirty
method.. or maybePostFlush
edit: also, can you post your code? Don't you get the Id as a parameter to
OnSave
?