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

发布于 2024-09-10 07:48:14 字数 581 浏览 9 评论 0原文

检索实体后,我更改它的属性。 然后我检索相同的实体。

我怎么说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-17 07:48:14

对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 updated LastName value will be reflected. If not, then you will need to make sure you are disposing your ISession instance each time to take advantage of auto flushing.

卷耳 2024-09-17 07:48:14

根据 文档 对于 Auto 的默认 FlushMode:

ISession 有时会被刷新
在查询执行之前,以便
确保查询永远不会返回过时的内容
状态。这是默认的刷新模式。

因此,您必须手动刷新会话,以确保在再次读取对象之前保留您的更改。

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

如果您的存储库对两个调用使用相同的 ISession(在我看来应该是这样),那么将从缓存中检索员工 4 并进行更改。但是,更改尚未保存到数据库中。

如果您的存储库 GetById 方法每次调用都使用新会话,那么它将始终访问数据库来检索员工。如果您在方法中处理会话,那么您的对象将以与会话分离的形式返回。这种策略违背了 NHibernate 的目的,并将其降级为简单的数据访问工具。

According to the documentation for the default FlushMode of Auto:

The ISession is sometimes flushed
before query execution in order to
ensure that queries never return stale
state. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文