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.