SaveOrUpdate 与 NHibernate 中的更新和保存 [英] SaveOrUpdate Vs Update and Save in NHibernate

查看:21
本文介绍了SaveOrUpdate 与 NHibernate 中的更新和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NHibernate 中的 SaveOrUpdate 和 Save/Update 有什么区别.为什么不总是使用 SaveOrUpdate?另外,SaveOrUpdateCopy 有什么意义?

What is the difference between SaveOrUpdate and Save/Update in NHibernate. Why wouldnt you just always use SaveOrUpdate? Also, what is the point of SaveOrUpdateCopy?

推荐答案

第 9 章比我能更好地涵盖所有这些:

Chapter 9 covers all of this better than I can:

http://nhibernate.info/doc/nh/en/index.html

但是悬崖笔记:

Save() 获取一个没有标识符的新对象并将其附加到会话中.该对象将被 INSERT'd.

Save() takes a new object without an identifier and attaches it to the session. The object will be INSERT'd.

Update() 获取具有标识符但不在会话中的现有对象并将其附加到会话.该对象将被UPDATE'd.

Update() takes an existing object that has an identifier but is not in the session and attaches it to the session. The object will be UPDATE'd.

SaveOrUpdate() 查看标识符并决定上面需要什么.

SaveOrUpdate() looks at the identifier and decides what is necessary in the above.

SaveOrUpdateCopy() 的特殊之处在于,您有两个具有相同标识符的对象——一个在会话中,一个不在.如果您尝试 update 不在会话中的那个,通常会抛出异常(您现在正在尝试将代表同一持久对象的两个对象附​​加到会话).SaveOrUpdateCopy() 将非会话对象状态复制到会话对象状态.

SaveOrUpdateCopy() is special in that say you have two objects with the same identifier -- one in the session and one not. If you try and update the one not in the session an exception is thrown normally (you are now trying to attach two objects that represent the same persistent object to the session). SaveOrUpdateCopy() copies the non-session object state to the session object state.

我不确定您将如何使用 NH,但在很多情况下,您只需要 Save().会话正在执行所有必要的工作,以了解必须更新的内容,只需 Flush()Commit() 即可完成您需要的一切.

I'm not sure how you are going to use NH, but for a lot of cases all you need is Save(). The session is doing ALL of the work necessary to know what has to be updated and simply Flush() or a Commit() does everything you need.

这篇关于SaveOrUpdate 与 NHibernate 中的更新和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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