SaveOrUpdate VS更新并保存在NHibernate中 [英] SaveOrUpdate Vs Update and Save in NHibernate

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

问题描述

SaveOrUpdate和NHibernate中的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添加.

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

Update()接受具有标识符但不在会话中的现有对象,并将其附加到会话.该对象将被UPDATE添加.

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 VS更新并保存在NHibernate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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