会话分离时仅更新 NHibernate 中更改的属性的最佳方法是什么? [英] What is the best approach to update only changed properties in NHibernate when session detached?

查看:13
本文介绍了会话分离时仅更新 NHibernate 中更改的属性的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用 NHibernate 的项目.我不保持会话打开.当我需要获取或保存对象时,我打开会话,执行我需要的操作,然后关闭会话.所以我一直在处理与会话分离的对象.

I am working on the project which uses NHibernate. I don't keep session opened. When i need to get or save object, i open the session, do what i need and then close the session. So all the time i'm working with objects detached from session.

例如,当我需要从数据库中获取对象时,我打开会话,然后调用 session.Get() 并关闭会话.然后我更新分离对象的一些属性.当我需要保存对数据库的更改时,我调用打开会话的方法,调用 session.Update(myObject) 并关闭会话.

For example, when i need to get object from the database, i open the session, then call session.Get() and close the session. Then i update some properties of detached object. When i need to save changes to the database i call method that opens session, calls session.Update(myObject) and closes the session.

但是当我这样做时,NHibernate 会生成 sql 来更新我映射的所有字段,即使它们没有改变.我的建议是当对象与会话分离时,NHibernate 无法跟踪所做的更改.当您只想更新从会话中分离的对象的已更改属性时,您使用什么方法?您如何跟踪分离对象的更改?

But when i do so, NHibernate generates sql that updates all the fields I have mapped, even though they have not changed. My suggestion is when objects is detached from session, NHibernate couldn't track the changes has been made. What approach do you use when you want to update only properties that has been changed for object detached from session? How do you track the changes for detached objects?

谢谢

推荐答案

问题是:你为什么要这样做?我认为如果您只更新已更改的列,这并不是什么优化.

The question is: why do you want to do this? I think it is not much of an optimization if you only update columns that have changed.

你在数据库中有触发器吗?

Have you got triggers in the database?

如果是这样,您可以执行以下操作:

If so, you can do the following:

  • 在映射中使用 select-before-update="true"dynamic-update="true".这使得 NH 在更新之前执行查询,并且仅在更改时更新,并且仅更新已更改的列.我不确定它是为每次更新选择,还是仅在它不在会话中时选择.
  • 使用 Merge 而不是更新.这实际上是一样的:它从数据库中选择实体,只有当它不在会话中时.还可以使用 dynamic-update="true".还有一个权衡:如果会话中已经有一个,Merge 返回附加的实例.因此,您应该始终丢弃传入的实例,并使用 Merge 中的实例.
  • use select-before-update="true" and dynamic-update="true" in the mapping. This makes NH to perform a query before the update and only updates if it changed, and only the columns that have changed. I'm not sure if it selects for every update, or only if it is not in the session.
  • use Merge instead of update. This does actually the same: it selects the entity from the database, only if it is not already in the session. Also use dynamic-update="true". There is also a trade-off: Merge returns the attached instance if there is already one in the session. So you should always throw away the instance you passed in and work with the instance you go from Merge.

实际上我不会关心更新的列.盲目更新它们很可能比执行前面的查询更快.

Actually I wouldn't care about the updated columns. Its most probably faster to update them blindly instead of performing a preceding query.

这篇关于会话分离时仅更新 NHibernate 中更改的属性的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆