java 休眠中的更新与合并方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49604134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:24:33  来源:igfitidea点击:

update vs merge method in hibernate

javahibernate

提问by Daniel

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.

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

Edit: 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.

编辑:如果我们通过 save 方法在持久化实例上调用 update 方法会发生什么?我以为 update 方法只用于分离的实例。

回答by Adya

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.

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

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: if you want to save your modifications at any time with out knowing about the state of an session, then use merge() in hibernate.

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

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.

当实体实例处于持久状态时,您对该实例的映射字段所做的所有更改都将在刷新会话时应用于相应的数据库记录和字段。持久实例可以被认为是“在线”的,而分离的实例已经“离线”并且不会被监控变化。

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.

这意味着当您更改持久对象的字段时,您不必调用 save、update 或任何这些方法来获取这些更改到数据库:您只需要提交事务,或者刷新或关闭会话,当你完成它。重要的是要了解所有方法(persist、save、update、merge、saveOrUpdate)不会立即产生相应的 SQL UPDATE 或 INSERT 语句。将数据实际保存到数据库发生在提交事务或刷新 Session 时

回答by Sumit Singh

In case of merge:When we call merge method on detached instance, it will update it with updated value.

在合并的情况下:当我们在分离的实例上调用合并方法时,它将用更新的值更新它。

In case of updateWhen we call update method on detached instance, it will give exception org.hibernate.NonUniqueObjectException

在更新的情况下当我们在分离的实例上调用更新方法时,它会给出异常org.hibernate.NonUniqueObjectException

回答by Nikhil Kamani

All the methods in hibernate

hibernate中的所有方法

  • save

  • merge

  • saveOrUpdate

  • Update

  • Delete

  • 保存

  • 合并

  • 保存或更新

  • 更新

  • 删除

does not immediately result in sql update or insert statments.

不会立即导致 sql 更新或插入语句。

The actual saving of data happens when we commit or flush the session.

数据的实际保存发生在我们提交或刷新会话时。