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

查看:88
本文介绍了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()的方法.

save()不保证相同,它 返回一个标识符,如果 必须执行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天全站免登陆