NHibernate和保存问题 - NHibernate不检测更改并使用旧值 [英] Problem with NHibernate and saving - NHibernate doesn't detect changes and uses old values

查看:151
本文介绍了NHibernate和保存问题 - NHibernate不检测更改并使用旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

  Cat x = Session.Load< Cat>(123); 
x.Name =fritz;
Session.Flush();

NHibernate检测到更改并更新数据库。但是,当我这样做时:

  Cat x = new Cat(); 
Session.Save(x);
x.Name =fritz;
Session.Flush();

我的名字是NULL,因为那是我在 Session时得到的结果.Save()。为什么NHibernate没有检测到更改 - 或者更好的是,在 Flush()?时获取INSERT语句的值?

新增:澄清: Session.FlushMode 设置为,所以没有自动刷新,直到我这样说。我也使用GUID主键(带有guid.comb生成器)。



我这样做的原因是因为我将Session用作肮脏跟踪器。我正在编写一个Windows Forms应用程序,并且我的每个表单都有一个单独的会话,持续时间与表单一样长。会话尽可能保持断开连接,这样我就不会用完ADO.NET连接(这是一个MDI应用程序,并且没有限制可以打开多少个表单)。每个表格还有一个确定按钮和一个取消按钮。 OK按钮调用 Session.Flush(),然后关闭表单; 取消按钮只是关闭表单,并默默地放弃用户所做的所有更改。



好吧,至少这是我想要的。上面的bug给了我一些问题。

解决方案

NHibernate确实跟踪了这些变化,但是你写的代码会导致它发出一个在调用保存时插入值,在调用保存后进行更改。保存使对象持久(插入),然后NHibernate开始跟踪对持久对象所做的更改(更新)。当会话被刷新时,插入和更新都会被提交。



我们正在面对Windows窗体应用程序的类似问题,我想我们要不同的方法。如果用户取消该操作,FlushMode会保留在自动(默认)状态下,并从会话中清除对象(ISession.Evict)。将物体逐出可以使其瞬间完全成为所需的行为。

When I do this:

Cat x = Session.Load<Cat>(123);
x.Name = "fritz";
Session.Flush();

NHibernate detects the change and UPDATEs the DB. But, when I do this:

Cat x = new Cat();
Session.Save(x);
x.Name = "fritz";
Session.Flush();

I get NULL for name, because that's what was there when I called Session.Save(). Why doesn't NHibernate detect the changes - or better yet, take the values for the INSERT statement at the time of Flush()?

Added: To clarify: The Session.FlushMode is set to None, so there are no automatic flushes until I say so. I'm also using GUID primary keys (with guid.comb generator).

The reason I'm doing this is because I'm using the Session as a "dirtiness" tracker. I'm writing a Windows Forms application and every one of my forms has a separate session which lasts as long as the form does. The session is kept disconnected as much as possible so that I don't run out of ADO.NET connections (it's an MDI application and there's no limit how many forms can be opened). Every form also has an "OK" button and a "Cancel" button. The "OK" button calls Session.Flush() and then closes the form; the "Cancel" button just closes the form and silently discards all changes the user has made.

Well, at least that's what I would like. The above bug is giving me problems.

解决方案

NHibernate does track the changes, but your code as written will cause it to issue an insert with the values at the time you called Save and an update for the changes made after calling Save. Save makes the object persistent (insert), then NHibernate begins tracking changes made to the persistent object (update). Both the insert and the update are committed when the session is flushed.

We're facing similar issues with our Windows Forms application and I think we're going to take a different approach. The FlushMode is left at Auto (default) and the object is evicted (ISession.Evict) from the session if the user cancels the operation. Evicting the object makes it transient which is exactly the desired behavior.

这篇关于NHibernate和保存问题 - NHibernate不检测更改并使用旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆