当我从不执行 session.Save 时,为什么 NHibernate 会保存对象?
我正在使用 NHibernate 和 Fluent NHibernate。
我有代码启动事务,然后进入一个创建多个对象的循环。对于每个对象,我都会检查某些条件。如果满足这些条件,则我对对象执行 session.SaveOrUpdate() 。在循环结束时,我发出一个提交事务。
我在 session.SaveOrUpdate 命令上设置了一个断点,证明它从未到达(因为循环中的任何对象都没有满足条件)。 尽管如此,当提交事务时,对象会被保存!
我正在使用 AuditInterceptor 并在 OnSave 方法中设置了断点。它正在被调用,但堆栈跟踪仅追溯到提交事务的语句。
此时没有任何类型的对象对其执行过 SaveOrUpdate,因此级联无法解释它。
为什么 NHibernate 保存这些对象?
I'm using NHibernate with Fluent NHibernate.
I have code where I start a transaction, then I enter a loop which creates several objects. For each object I check certain conditions. If these conditions are met, then I execute a session.SaveOrUpdate() on the object. At the end of the loop, I issue a commit transaction.
I have a breakpoint set on the session.SaveOrUpdate command, proving that it is never reached (because the conditions have not been met by any of the objects in the loop). Nevertheless, when the transaction is committed, the objects are saved!
I am using an AuditInterceptor and have set a breakpoint in the OnSave method. It is being called, but the stack trace only traces back to the statement that commits the transaction.
There are no objects of any kind that have had SaveOrUpdate executed on them at this point, so cascading doesn't explain it.
Why is NHibernate saving these objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 NHibernate ISession.Update 线程:
这是 正常和默认行为:
来自 Hibernate 陷阱第 2 部分:
来自 NHibernate 的自动(脏检查)更新行为<强>:
From NHibernate ISession.Update thread:
It's the normal and default behavior:
From Hibernate Pitfalls part 2:
From NHibernate's automatic (dirty checking) update behaviour:
这与会话刷新模式为 FlushMode.Commit (默认)有关。当提交事务时,对会话中的对象所做的任何更改都会被保存,并且这些更改将持续存在。
您可以设置会话上的 FlushMode 属性。如果您想要只读事务,请指定 FlushMode.Manual。
希望这有帮助!
It's to do with the sessions flush mode being FlushMode.Commit (default). When the transaction is committed any changes made to objects within the session are saved and the changes persisted.
There's a FlushMode property on the session that you can set. If you want a readonly transaction specify FlushMode.Manual.
Hope this helps!