NHibernate FlushMode:如何设置 NHibernate 以自动更新实体
检索实体后,我更改它的属性。 然后我检索相同的实体。
我怎么说 Nhibernate,它应该在加载实体之前更新实体?
这里是代码:
EmployeeRepository employeeRepository = new EmployeeRepository();
Employee employee = employeeRepository.GetById(4);
employee.LastName = "TEST!!!";
Employee employee2 = employeeRepository.GetById(4);
目前 Nhibernate 没有在我的程序中进行更新。我认为只需将 FlushMode 设置为 Auto 就会自动更新实体。
编辑 背景是我尝试在另一个应用程序中重现此行为。 没有保存方法!就这个代码。 NHibernate 版本确实很旧,版本是 1.2.1.4000。也许有一个问题。
当我将棕地应用程序中的 FlushMode 设置为 Commit 时,不会生成更新语句。
但在我自己的项目中,我仍然无法重现这种“自动”行为。
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?
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. I thought just setting the FlushMode to Auto will update the entity automatically.
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. maybe there is the catch.
When I set the FlushMode in the brownfield application to Commit then no update statement is generated.
But in my own project I still can not reproduce this "automatic" behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对employeeRepository 的两次调用最终是否使用相同的NHibernate
ISession
实例?如果是这样,那么它们将返回相同的对象,并且更新的LastName
值将得到反映。如果没有,那么您需要确保每次都处理您的ISession
实例以利用自动刷新。Are both calls to the employeeRepository ultimately using the same NHibernate
ISession
instance? If so, then they will return the same object, and the updatedLastName
value will be reflected. If not, then you will need to make sure you are disposing yourISession
instance each time to take advantage of auto flushing.根据 文档 对于 Auto 的默认 FlushMode:
因此,您必须手动刷新会话,以确保在再次读取对象之前保留您的更改。
如果您的存储库对两个调用使用相同的 ISession(在我看来应该是这样),那么将从缓存中检索员工 4 并进行更改。但是,更改尚未保存到数据库中。
如果您的存储库 GetById 方法每次调用都使用新会话,那么它将始终访问数据库来检索员工。如果您在方法中处理会话,那么您的对象将以与会话分离的形式返回。这种策略违背了 NHibernate 的目的,并将其降级为简单的数据访问工具。
According to the documentation for the default FlushMode of Auto:
So you have to manually flush the session to ensure that your changes are persisted before reading the object again.
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. 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. 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.