开发者

NHibernate FlushMode - Why Can't NH Evict Everything Not Saved?

开发者 https://www.devze.com 2022-12-22 05:27 出处:网络
I\'ve spent some time searching around how to configure NHibernate\'s FlushMode so it could only save objects that I\'ve explicity called Save/Update/Delete, but I\'ve figured out that I can\'t do tha

I've spent some time searching around how to configure NHibernate's FlushMode so it could only save objects that I've explicity called Save/Update/Delete, but I've figured out that I can't do that. Instead of it, I have to evict every object that开发者_运维百科 I've modified (even without calling Save/Update/Delete) as I'm using NHibernate transaction management.

I understand perfectly why NHibernate have to Flush some objects before some Find operations, but I'm not worried about stale data. I see that, maybe, in some situation, flush everything that was modified and not explicity saved can be usefull, but it's not my case.

I simply want that, after commiting my session, NHibernate inserts/updates/deletes everything that I have explicitly demanded it to, and evict everything else. My question is: is this behavior just a question of "nobody stopped to implement this yet" or are there another points that would fail if this kind of behavior existed?

Thank you in advance.

Filipe


Nhibernate doesn't think that way. The distinction is between transient and persistent objects, and persistent objects are synchronized with the database when the session is flushed (an possibly at other times). Objects that are retrieved using NH are persistent and will be saved when the session is flushed without calling Save (or SaveOrUpdate) because they are already persistent. There are several options for controlling the FlushMode but nothing that would make it work the way you desire.

A potential workaround might be to retrieve objects using an IStatelessSession and handle operations through a separate ISession.

What problem are you trying to solve?


You are basically asking: "why doesn't my hammer work more like a screwdriver?"

The whole idea of the Session (among other things) is to allow automatic dirty tracking, so you don't need to worry about what was changed; only additions and non-cascaded deletions are manual.

As Jamie mentioned you can use IStateLessSession instead of ISession. It does not track anything automatically and it does not support lazy loading. You have to tell it explicitly what to insert, update and delete. It's more used for read-only and batch contexts, but it's probably what you're looking for,

0

精彩评论

暂无评论...
验证码 换一张
取 消