保存时不使用 NHibernate 拦截器
我有一个使用 Spring.Net 在 HibernateTemplate 中设置的 NHibernate 拦截器(我使用的是 ASP.NET MVC,fwiw),它用于审核。但是,由于某种原因,虽然当我调用 genericDAO.Get(id) 时会触发 OnLoad 方法,但当我尝试使用 genericDAO.SaveOrUpdate(object) 保存某些内容时,OnSave 或 OnFlushDirty 拦截器方法都不会被调用。有人知道为什么会这样吗?
我已经在 VS 调试器中的方法上设置了断点,因此我非常确定拦截器已设置并且我没有错过任何方法调用。显然,所有内容也都被正确保存和检索。
public class AuditInterceptor
: EmptyInterceptor
{
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
// Implementation
// Called when using genericDAO.Get(id)
}
public override bool OnSave(Object entity, Object id, Object[] state,
String[] propertyNames, IType[] types)
{
// Implementation
// NOT called when using genericDAO.SaveOrUpdate(entity)
}
public override bool OnFlushDirty(Object entity, Object id,
Object[] currentState, Object[] previousState,
String[] propertyNames, IType[] types)
{
// Implementation
}
}
I have a NHibernate Interceptor that is set in the HibernateTemplate using Spring.Net (I'm using ASP.NET MVC, fwiw), which is used for Auditing. However, for some reason, while the OnLoad method is being triggered when I call genericDAO.Get(id), when I try to save something using genericDAO.SaveOrUpdate(object) neither the OnSave or OnFlushDirty Interceptor methods are called. Anyone know why this might be?
I've set breakpoints on the methods within VS debugger, so I'm pretty certain the Interceptor is set and that I'm not missing any method calls. Obviously, everything is being saved and retrieved correctly too.
public class AuditInterceptor
: EmptyInterceptor
{
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
// Implementation
// Called when using genericDAO.Get(id)
}
public override bool OnSave(Object entity, Object id, Object[] state,
String[] propertyNames, IType[] types)
{
// Implementation
// NOT called when using genericDAO.SaveOrUpdate(entity)
}
public override bool OnFlushDirty(Object entity, Object id,
Object[] currentState, Object[] previousState,
String[] propertyNames, IType[] types)
{
// Implementation
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的objects.xml 文件的精简(匿名)版本:
This is a cut-down (and anonymised) version of my objects.xml file:
看来你没有使用交易。您应该尝试使用它们,如 Spring Nhibernate 示例(例如:)中所示,并激活日志记录以真正查看会话是否被刷新。
您还声明了两个会话工厂。我猜这不相关,但这是故意的吗?
另外,Java spring 的相当旧的线程可能会有所帮助。
It seems that you do not use transactions. You should try using them as shown in the Spring Nhibernate sample (e.g.:) and activate logging to really see if the session gets flushed.
Also you are declaring two session factories. It is not related I guess but is this intentional?
Also this rather old thread for Java's spring might be helpful.