Hibernate坚持vs保存 [英] Hibernate persist vs save

查看:97
本文介绍了Hibernate坚持vs保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




persist()已定义良好。它使持久的瞬态实例。
但是,它不能保证标识符值将立即分配给持久化实例的
,分配可能
发生在刷新时间。规范没有说,这是问题
我坚持()。
$ b persist()还保证,如果在事务边界之外调用它,它将不会执行INSERT语句
。这在使用扩展会话/持久性
上下文的
长时间运行对话中很有用。


像persist()这样的方法是必需的。
$ b $ save不保证相同,它返回一个标识符,如果需要执行
INSERT来获得标识符(例如identity
生成器,而不是序列),如果您处于事务内部或外部,则此INSERT立即发生,无论
如何。这在长时间运行
的会话中具有延长的会话/持久性
上下文并不好。


Can你需要帮助我理解持续表达的行:
$ b


persist()也保证它不会执行INSERT语句
如果它在事务边界之外被调用。这在使用扩展会话/持久性
上下文的
长时间运行对话中很有用。

什么是交易界限在这里?什么是长时间运行的对话?什么是扩展会话/持久化上下文意味着什么?



另外对于保存方法:


这个INSERT立即发生,不管你是在交易之外还是
。这在长时间运行的
对话中不是很好,它具有扩展的会话/持久化上下文。


我知道我们需要没有像session.beginTransaction()和session.getTransaction()。commt()这样的语句,如果我在程序中使用save方法保存对象。请让我知道,如果声明在这里说同样的事情。那么这对长时间运行的对话有什么用处呢?



我是hibernate的新手,发现难以理解差异,请您帮助我理解差异。 / p>

解决方案

您的问题涉及在View中打开会话模式Hibernate的实现。



这个想法是,您可能需要一个Web应用程序中的原子工作单元,在整个特定过程中运行。想象一下在线订购食物。您可以在一个页面上登录,在下一页选择披萨和浇头,在下一页上添加点心,在下一页添加饮料,并在最后一页上付款。你希望整个过程成为一个工作单元。



因此,Hibernate Session 需要在该工作单元的开始并在最后关闭 - 无论是手动还是通过某种容器管理。

在对话期间调用 persist 不会导致任何数据插入,但会使分离的实体持久化。 Hibernate将记录所有插入的内容,然后在对话结束时刷新它们。



同时 save 立即执行插入操作,并为数据库中的实体提供一个id。在长时间运行的对话中,这是不好的,因为你希望你的数据库操作是原子的 - 全部或全部。奇怪的事情,如多次插入相同的数据可能会发生。



希望有帮助。


I came across this explanation when I was trying to understand between Hibernate Save and 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() 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.

A method like persist() is required.

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.

Can you please help me in understanding the lines for persist that says:

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.

What are the transaction boundaries here? and what are the long-running conversations? what is extended Session/persistence context means?

Also for save method:

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.

I understand that we need not have statements like session.beginTransaction() and session.getTransaction().commt() if we are using save method in my program for saving an object. Please let me know if the statement says the same thing here. So how is this useful in long running conversations?

I am new to hibernate and finding it difficulty to understand the differences, can you please help me in understanding the differences.

解决方案

Your question relates to the Open Session in View pattern Hibernate implements.

The idea is that you might have an atomic unit of work in a web application that needs to run throughout a particular process. Imagine ordering food online. You sign in on one page, choose pizza and toppings on the next page, add dessert on the next page, add drinks on the next page, and pay on the last page. You want that whole process to be a single unit of work.

So the Hibernate Session needs to be opened at the beginning of that unit of work and closed at the end--either manually or through some kind of container management.

Calling persist during that conversation will not result in any inserts of data, but it will make a detached entity persistent. Hibernate will "keep a record" of all inserts to be made and then flush them at the end of the conversation.

Meanwhile save does the insert immediately and gives your entity an id from the database. This isn't good during a long-running conversation because you want your database operations to be atomic--all or nothing. Weird things like multiple inserts of the same data could occur.

Hope that helps.

这篇关于Hibernate坚持vs保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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