简体   繁体   English

NHibernate FlushMode - 为什么NH不能驱逐所有未保存的东西?

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

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. 我花了一些时间来搜索如何配置NHibernate的FlushMode,因此它只能保存我明确称为Save / Update / Delete的对象,但我发现我不能这样做。 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. 而不是它,我必须驱逐我修改过的每个对象(即使没有调用Save / Update / Delete),因为我正在使用NHibernate事务管理。

I understand perfectly why NHibernate have to Flush some objects before some Find operations, but I'm not worried about stale data. 我完全理解为什么NHibernate必须在一些Find操作之前刷新一些对象,但我并不担心过时的数据。 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. 我只是想要,在提交我的会话后,NHibernate插入/更新/删除我明确要求的所有内容,并逐出其他所有内容。 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. Nhibernate并不这么认为。 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. 使用NH检索的对象是持久的,并且在刷新会话时将保存而不调用Save(或SaveOrUpdate),因为它们已经是持久的。 There are several options for controlling the FlushMode but nothing that would make it work the way you desire. 控制FlushMode有几种选择,但没有任何选项可以让它按照你想要的方式工作。

A potential workaround might be to retrieve objects using an IStatelessSession and handle operations through a separate ISession. 可能的解决方法可能是使用IStatelessSession检索对象并通过单独的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. 正如Jamie所提到的,你可以使用IStateLessSession而不是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, 它更多地用于只读和批处理上下文,但它可能正是你要找的,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM