简体   繁体   English

NHibernate FlushMode:如何设置NHibernate以自动更新实体

[英]NHibernate FlushMode: How do I set up NHibernate for automatically updating an entity

After I retrieve an entity, I change a property of it. 检索实体后,更改其属性。 Then I retrieve the same entity. 然后,我检索相同的实体。

How do I say Nhibernate, that it shall update the entity before it loads the entity? 我怎么说Nhibernate,它将在加载实体之前更新实体?

Here the code: 这里的代码:

EmployeeRepository employeeRepository = new EmployeeRepository();
Employee employee = employeeRepository.GetById(4);
employee.LastName = "TEST!!!";
Employee employee2 = employeeRepository.GetById(4);

Currently Nhibernate don't make an update in my program. 目前,Nhibernate不在我的程序中进行更新。 I thought just setting the FlushMode to Auto will update the entity automatically. 我以为只是将FlushMode设置为Auto会自动更新实体。

EDIT The background is that I try to reprdouce this behaviour in another application. 编辑背景是我尝试在另一个应用程序中重做此行为。 There is NO save method! 没有保存方法! Just this code. 仅此代码。 The NHibernate version is really old, it is version 1.2.1.4000. NHibernate版本真的很旧,它是1.2.1.4000版本。 maybe there is the catch. 也许有收获。

When I set the FlushMode in the brownfield application to Commit then no update statement is generated. 当我在Brownfield应用程序中将FlushMode设置为Commit时,不会生成任何更新语句。

But in my own project I still can not reproduce this "automatic" behaviour. 但是在我自己的项目中,我仍然无法重现这种“自动”行为。

Are both calls to the employeeRepository ultimately using the same NHibernate ISession instance? 最终都使用相同的NHibernate ISession实例对employeeRepository的两个调用吗? If so, then they will return the same object, and the updated LastName value will be reflected. 如果是这样,则它们将返回相同的对象,并且将反映更新的LastName值。 If not, then you will need to make sure you are disposing your ISession instance each time to take advantage of auto flushing. 如果没有,那么您将需要确保每次都在处置ISession实例,以利用自动刷新的优势。

According to the documentation for the default FlushMode of Auto: 根据默认FlushMode为Auto 的文档

The ISession is sometimes flushed before query execution in order to ensure that queries never return stale state. 有时会在执行查询之前刷新ISession,以确保查询永远不会返回陈旧状态。 This is the default flush mode. 这是默认的刷新模式。

So you have to manually flush the session to ensure that your changes are persisted before reading the object again. 因此,您必须手动刷新会话以确保所做的更改得以保留,然后再次读取该对象。

EmployeeRepository employeeRepository = new EmployeeRepository();
Employee employee = employeeRepository.GetById(4);
employee.LastName = "TEST!!!";
session.Flush();
Employee employee2 = employeeRepository.GetById(4);

If your repository is using the same ISession for both calls (as it should imo) then employee 4 will be retrieved from the cache and have the change. 如果您的存储库对两个调用都使用相同的ISession(应为imo),则将从缓存中检索出雇员4并进行更改。 However, the change will not have been persisted to the database yet. 但是,所做的更改尚未保存到数据库中。

If your repository GetById methods uses a new session for each call then it will always hit the database to retrieve the employee. 如果您的存储库GetById方法为每个调用使用一个新会话,则它将始终命中数据库以检索员工。 If you're disposing of the session in the method then your objects are returned as detached from a session. 如果要在方法中处理会话,则对象将与会话分离。 This strategy defeats the purpose of NHibernate and relegates it to a simple data access tool. 这种策略无法实现NHibernate的目的,而是将其委托给一个简单的数据访问工具。

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

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