Hibernate 中的 session.persist() 和 session.save() 有什么区别? [英] What's the difference between session.persist() and session.save() in Hibernate?

查看:39
本文介绍了Hibernate 中的 session.persist() 和 session.save() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我 persist()save() 在 Hibernate 中的优势是什么?

Can anyone tell me what's the advantage of persist() vs save() in Hibernate?

推荐答案

来自 此论坛帖子

persist() 定义良好.它使一个瞬态实例持久化.但是,它并不能保证标识符值将被分配给立即持久化实例,分配可能发生在刷新时间.规范没有说,哪个是我在使用 persist() 时遇到的问题.

persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().

persist() 也保证它会不执行 INSERT 语句,如果它在事务之外被调用边界.这在与一个长期运行的对话扩展会话/持久性上下文.

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

需要像 persist() 这样的方法.

A method like persist() is required.

save() 不保证相同,它返回一个标识符,如果必须执行 INSERT 才能获得标识符(例如身份"生成器,不是序列"),这个 INSERT 发生立即,无论您是在交易内部或外部.这在长期运行中是不好的与一个扩展的对话会话/持久性上下文.

save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.

这篇关于Hibernate 中的 session.persist() 和 session.save() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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