简体   繁体   English

休眠保持与保存

[英]Hibernate persist vs save

I came across this explanation when I was trying to understand between Hibernate Save and persist: 当我试图了解Hibernate Save和坚持时,我遇到了这个解释:

persist() is well defined. persist()定义明确。 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()问题。

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. persist()还保证如果在事务边界之外调用它,则不会执行INSERT语句。 This is useful in long-running conversations with an extended Session/persistence context. 这在具有扩展会话/持久性上下文的长时间对话中很有用。

A method like persist() is required. 需要诸如persist()之类的方法。

save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (eg "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. save()不能保证相同,它返回一个标识符,并且如果必须执行INSERT来获取标识符(例如,“ identity”生成器,而不是“ sequence”),则无论您是否在内部,该INSERT都会立即发生或交易之外。 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. persist()还保证如果在事务边界之外调用它,则不会执行INSERT语句。 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. 无论您是在事务内部还是外部,此INSERT都会立即发生。 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. 我了解,如果我们在程序中使用save方法来保存对象,则不需要像session.beginTransaction()和session.getTransaction()。commt()这样的语句。 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. 您的问题与Hibernate实现的View模式下的Open Session有关

The idea is that you might have an atomic unit of work in a web application that needs to run throughout a particular process. 这个想法是,您可能需要在整个特定过程中运行Web应用程序中的原子工作单元。 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. 因此,Hibernate Session需要在该工作单元的开始处打开,然后在结束时关闭–可以手动或通过某种容器管理进行。

Calling persist during that conversation will not result in any inserts of data, but it will make a detached entity persistent. 在该对话期间调用persist不会导致任何数据插入,但会使分离的实体持久。 Hibernate will "keep a record" of all inserts to be made and then flush them at the end of the conversation. Hibernate将“保留所有要插入的记录”,然后在对话结束时将其刷新。

Meanwhile save does the insert immediately and gives your entity an id from the database. 同时save立即执行插入操作,并从数据库中为您的实体提供ID。 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. 希望能有所帮助。

The save() method can return that primary key id value which is generated by hibernate and we can see it by save()方法可以返回由hibernate生成的主键id值,我们可以通过以下方式查看它:

long s = session.save(k);

In this same case, persist() will never give any value back to the client, hope you are clear. 在这种情况下, persist()永远不会给客户端任何价值,希望您一切都清楚。

So persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries since there is no need to get id after persisting an object. 因此, persist()方法可确保如果在事务边界之外调用它,则不会执行INSERT语句,因为在持久化对象后无需获取ID。

Whereas the save() method does not guarantee the same, since it need to returns an identifier (probably primary key id). save()方法不能保证相同,因为它需要返回一个标识符(可能是主键ID)。

An INSERT has to be executed to get the identifier (eg "identity" generator), So, this INSERT happens immediately with save() , no matter if you are inside or outside of a transaction. 必须执行INSERT来获取标识符(例如“身份”生成器),因此,无论您在事务内部还是外部,此INSERT都会通过save()立即发生。

One of the key difference between save and persist methods are depends on ID generation strategies. 保存和持久方法之间的主要区别之一取决于ID生成策略。 Basically save() method will return the value(id) back to the client and persist() method will not.. 基本上,save()方法会将value(id)返回给客户端,而persist()方法则不会。

1) In a case where you let your ORM engine generates ID for you and in those cases you can use save().Reason is simple "Hibernate giving you the opportunity to get to know what ID it has generated/used to persist your object " 1)在让您的ORM引擎为您生成ID的情况下,您可以使用save()。原因很简单:“休眠使您有机会了解它已生成/使用了什么ID来持久化对象”

2) In a case where your have generated your id by yourself (assigned ID generation strategy) and passing it to ORM engine to use the same to persist your object.In such cases persist() will be appropriate . 2)如果您自己生成了自己的ID(分配的ID生成策略),并将其传递给ORM引擎以使用该ID来持久化您的对象,在这种情况下,persist()将是合适的。

click here 点击这里

As I said 就像我说的

"It depends on ID generation strategies" “取决于ID生成策略”

Cheers! 干杯!

As the method name suggests, hibernate save() can be used to save entity to database. 顾名思义,hibernate save()可用于将实体保存到数据库。 We can invoke this method outside a transaction. 我们可以在事务外部调用此方法。 If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session 如果我们在不使用事务的情况下使用此方法,并且在实体之间进行级联,那么只有主实体才能保存,除非刷新会话

Hibernate persist is similar to save (with transaction) and it adds the entity object to the persistent context, so any further changes are tracked. Hibernate持久性类似于保存(带有事务),并且将实体对象添加到持久性上下文中,因此可以跟踪任何进一步的更改。 If the object properties are changed before the transaction is committed or session is flushed, it will also be saved into database. 如果在提交事务或刷新会话之前更改了对象属性,则该对象属性还将保存到数据库中。 Also, we can use persist() method only within the boundary of a transaction, so it's safe and takes care of any cascaded objects. 另外,我们只能在事务的边界内使用persist()方法,因此它是安全的,并且可以处理所有级联的对象。 Finally, persist doesn't return anything so we need to use the persisted object to get the generated identifier value. 最后,persist不返回任何内容,因此我们需要使用persisted对象来获取生成的标识符值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM