休眠中更新与合并方法 [英] update vs merge method in hibernate

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

问题描述

我知道,如果没有其他具有相同ID和类型的对象附加到会话中,则使用update会将分离的对象置于持久状态.合并不关心状态.如果会话中不存在相同类型的持久对象,或者它使用新对象的值更新旧对象,则它仅返回一个持久对象.我的问题是关于数据库命中的问题.方法更新"和合并"是否会立即命中数据库?或关闭会话后,数据库中的更改显而易见.

I understand that update is used to put a detached object into persistent state if no other object with the same id and type is attached to the session. Merge doesn't care about states. It just returns a persisted object of the same type if it doesn't exist in the session or it updates the old object with the values of the new object. My questions is regarding database hits. Does the method 'update' and 'merge' hit the database immediately? or changes are made apparent in the database when the session is closed.

如果我们通过save方法在持久化实例上调用update方法,会发生什么情况?我以为更新方法仅用于分离的实例.

What happens if we call the update method on a persisted instance by the save method?. I thought the update method was just used on detached instances.

推荐答案

刷新会话后,Hibernate会处理对会话中对象的任何更改.如果对象的实例已经在会话中,则更新可能会失败.在这种情况下,应使用合并.它将分离对象的更改与会话中的对象(如果存在)合并.

Hibernate handles persisting any changes to objects in the session when the session is flushed. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists.

更新:如果您确定会话中不包含具有相同标识符的已持久实例,则可以使用update将数据保存在休眠状态

Update: if you are sure that the session does not contains an already persistent instance with the same identifier,then use update to save the data in hibernate

合并:如果您想在不了解会话状态的情况下随时保存所做的修改,请在休眠状态下使用merge().

Merge: if you want to save your modifications at any time with out knowing about the state of an session, then use merge() in hibernate.

当实体实例处于持久状态时,在刷新会话时,您对此实例的映射字段所做的所有更改都将应用于相应的数据库记录和字段.可以将持久性实例视为联机",而分离的实例已变为脱机"并且不受监视以进行更改.

When the entity instance is in the persistent state, all changes that you make to the mapped fields of this instance will be applied to the corresponding database records and fields upon flushing the Session. The persistent instance can be thought of as "online", whereas the detached instance has gone "offline" and is not monitored for changes.

这意味着,当您更改持久对象的字段时,无需调用save,update或任何这些方法即可将这些更改获取到数据库:您所需要做的只是提交事务,刷新或完成后,关闭会话. 重要的是要了解所有方法(持久性,保存,更新,合并,saveOrUpdate)不会立即导致相应的SQL UPDATE或INSERT语句. 实际将数据保存到数据库是在提交事务或刷新会话时发生的..

This means that when you change fields of a persistent object, you don’t have to call save, update or any of those methods to get these changes to the database: all you need is to commit the transaction, or flush or close the session, when you’re done with it. It is important to understand that all of the methods (persist, save, update, merge, saveOrUpdate) do not immediately result in the corresponding SQL UPDATE or INSERT statements. The actual saving of data to the database occurs on committing the transaction or flushing the Session.

这篇关于休眠中更新与合并方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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