java 什么是 Hibernate 脏会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10804676/
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-10-31 02:39:32  来源:igfitidea点击:

What is a Hibernate dirty session?

javahibernate

提问by Code Junkie

I was wondering if anybody could tell me what a hibernate dirty session is? I seem to be having an issue where a criteria query is performing an insert when it shouldn't. I believe it's related to a dirty session, but without knowing truly what a dirty session is, I'm unable to resolve my issue. Also, how do you create a dirty session. Thanks.

我想知道是否有人可以告诉我什么是休眠脏会话?我似乎遇到了一个问题,即标准查询在不应该执行插入时执行插入。我相信它与脏会话有关,但在不真正知道脏会话是什么的情况下,我无法解决我的问题。另外,您如何创建脏会话。谢谢。

回答by JB Nizet

The Hibernate session is a cache. It caches entities read from the database, and it also caches changes you've made to entities it contains, as well as added and removed entities, until the session is flushed (i.e. all the pending changes are written to the database).

Hibernate 会话是一个缓存。它缓存从数据库读取的实体,它还缓存您对它包含的实体所做的更改,以及添加和删除的实体,直到刷新会话(即所有挂起的更改都写入数据库)。

A session is said dirtywhen some changes have not been flushed yet. And it's thus perfectly normal to have a dirty session. The session is flushed before the transaction is committed.

当某些更改尚未刷新时,会话被称为会话。因此,有一个肮脏的会话是完全正常的。在提交事务之前刷新会话。

回答by richarbernal

A dirty session in Hibernate is when you load an object inside the session and then modifies it.

Hibernate 中的脏会话是当您在会话中加载一个对象然后修改它时。

Or, when you open a session and create a new object.

或者,当您打开会话并创建新对象时。

Even if you don't explicitily call any insert/update operation, Hibernate marks the session as dirtyand saves the changes when the session is closed

即使您没有明确调用任何插入/更新操作,Hibernate 也会将会话标记为并在会话关闭时保存更改

回答by Sonu

In simple words: As we know Dirty data is the one which is not yet committed. In the same way dirty session in hibernate contains modified data which is not yet committed.

简单来说:正如我们所知,脏数据是尚未提交的数据。以同样的方式,休眠中的脏会话包含尚未提交的修改数据。

回答by Affe

It just means you have made changes to in-memory, managed, persistent objects, that have not yet been flushed to the database.

这只是意味着您对尚未刷新到数据库的内存中的托管持久对象进行了更改。

Basically the idea behind hibernate is that the state of the in memory persistent objects isthe state of the application. If you make a change to a managed object, hibernate is going to put that in the database at the next opportunity. You should not make 'temporary' changes to managed objects that you do not intend to become persistent, because they will be!

基本上后面休眠的想法是,在内存中的持久对象的状态应用程序的状态。如果您对托管对象进行更改,hibernate 将在下一次机会将其放入数据库中。您不应该对不打算持久化的托管对象进行“临时”更改,因为它们会持久化!

Before executing a query, hibernate flushes the in memory state of all managed objects to the database, so that the query is accurate relative to the state of your application.

在执行查询之前,hibernate 会将所有托管对象的内存状态刷新到数据库,以便查询相对于应用程序的状态是准确的。