Java 什么是休眠中的分离、持久和瞬态对象?

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

What are detached, persistent and transient objects in hibernate?

javahibernateorm

提问by Jigar Joshi

What are detached, persistent and transient objects in hibernate? Please explain with an example.

什么是休眠中的分离、持久和瞬态对象?请举例说明。

采纳答案by Pascal Thivent

A newinstance of a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transientby Hibernate:

new其不与相关联的永久类的实例Session,有在数据库中没有表示和没有标识符值被认为是瞬时的休眠:

Person person = new Person();
person.setName("Foobar");
// person is in a transient state

A persistentinstance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistentby associating it with a Session:

持久实例具有在数据库中的表示,标识符值,并与一个相关联Session。您可以通过将瞬态实例与 a 关联来使其持久化Session

Long id = (Long) session.save(person);
// person is now in a persistent state

Now, if we closethe Hibernate Session, the persistent instance will become a detachedinstance: it isn't attached to a Sessionanymore (but can still be modified and reattached to a new Sessionlater though).

现在,如果我们closeHibernate Session,持久实例将成为一个分离的实例:它不再附加到 a Session(但仍然可以修改并重新附加到一个新的Session稍后)。

All this is clearly explained in the whole Chapter 10. Working with objectsof the Hibernate documentation that I'm only paraphrasing above. Definitely, a must-read.

所有这些在整个第 10 章中都有清楚的解释。使用Hibernate 文档中的对象,我只是在上面释义。绝对,必读。

回答by Amol Dixit

Beside the correct answer already identified persistent, transient, detached are just the state of the object in hibernate.

除了已经确定的正确答案外,持久、瞬态、分离只是对象在休眠状态下的状态。

To be more precise, these three states actually show the hibernate object changes and the session life cycle status

更准确的说,这三个状态实际上显示了hibernate对象的变化和会话生命周期状态

回答by Sk Sharma

Object in hibernate has following states:

休眠中的对象具有以下状态:

Transient - Objects instantiated using the new operator are called transient objects.

Transient - 使用 new 运算符实例化的对象称为瞬态对象。

An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore.

如果一个对象刚刚使用 new 操作符实例化,并且它不与 Hibernate Session 相关联,则该对象是瞬态的。它在数据库中没有持久表示,也没有分配标识符值。如果应用程序不再持有引用,垃圾收集器将销毁瞬态实例。

Persistent - An object that has a database identity associated with it is called a persistent object.

持久性 - 具有与其关联的数据库标识的对象称为持久性对象。

A persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded; however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes.

持久实例在数据库中有一个表示和一个标识符值。它可能刚刚被保存或加载;但是,根据定义,它属于 Session 的范围。Hibernate 将检测对处于持久状态的对象所做的任何更改,并在工作单元完成时将状态与数据库同步。

Detached - A detached instance is an object that has been persistent, but its Session has been closed.

Detached - 分离的实例是一个持久化的对象,但它的 Session 已经关闭。

A detached instance can be reattached to a new Session at a later point in time, making it persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

分离的实例可以在稍后的时间点重新附加到新的会话,使其再次持久化。此功能为需要用户思考时间的长时间运行的工作单元启用编程模型。我们称它们为应用事务,即从用户的角度来看的一个工作单元。

http://webiwip.com/interview-questions-answers/hibernate-interview-questions/32012

http://webiwip.com/interview-questions-answers/hibernate-interview-questions/32012

回答by Sabunkar Tejas Sahailesh

Let me explain in Garbage collector point of view also.

让我也从垃圾收集器的角度解释一下。

There are 3 Object states of hibernate (or) Object Scope of hibernate-

有 3 种休眠的对象状态(或)休眠的对象范围 -

  1. Transient state
  2. persistent state
  3. detached state
  1. 瞬态
  2. 持久状态
  3. 分离状态

It is better to understand with a code example-

最好通过代码示例来理解-

Let us consider a POJO class as Student Object->

让我们将 POJO 类视为 Student Object->

Student student = new Student(); 

Now, this student object is at transient state.

现在,这个学生对象处于瞬态



When we attache this POJO object to hibernate session->

当我们将此 POJO 对象附加到休眠会话时->

session.save(student);

Now this POJO object is at persistent state.

现在这个 POJO 对象处于持久状态

(Garbage collector point of view- GC cannot wipe-out Any object which is in the persistent state. Soo we can say that persistent state is like temporary storage for POJO objects)

(垃圾收集器的观点- GC 不能清除任何处于持久状态的对象。所以我们可以说持久状态就像POJO 对象的临时存储



If we perform->

如果我们执行->

session.beginTransaction.commit();

then the POJO object is at Permanent or Database storage state

那么 POJO 对象处于永久或数据库存储状态

(Garbage collector point of view- GC cannot wipe-out this object because this POJO object is now outside the scope of JVM and stored in the form table inside a database.Soo we can say that this Database storage state is like permanent storage for POJO objects)

(垃圾收集器的观点——GC 无法清除这个对象,因为这个 POJO 对象现在不在 JVM 的范围内,存储在数据库内部的表单表中。所以我们可以说这个数据库存储状态就像POJO 的永久存储对象



If we perform->

如果我们执行->

session.evict(student); 

then POJO object is evicted or removed back from the persistent state to detached state.Soo this state of POJO object is detached state.

然后POJO对象被驱逐或从持久状态移除回分离状态。所以POJO对象的这种状态是分离状态

(Garbage collector point of view- GC can easily wipe-out the detached state POJO object from JVM)

(垃圾收集器的观点——GC可以很容易地从JVM中清除掉处于分离状态的POJO对象)

回答by O.Badr

Given the following entity:

给定以下实体:

@Entity
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private long id;

    // other fields and methods.
}

From Hibernate 5.2 documentation(I've also included the removedstate):

来自Hibernate 5.2 文档(我还包括removed状态):

transient

the entity has just been instantiated and is not associated with a persistence context. It has no persistent representation in the database and typically no identifier value has been assigned (unless the assigned generator was used).

短暂的

该实体刚刚被实例化,并没有与持久化上下文相关联。它在数据库中没有持久表示,并且通常没有分配标识符值(除非使用了分配的生成器)。

City city = new City();

managed, or persistent

the entity has an associated identifier and is associated with a persistence context. It may or may not physically exist in the database yet.

管理或持续

该实体有一个关联的标识符并与一个持久化上下文关联。它可能物理存在也可能不存在于数据库中。

// city will be in a managed/persistent state and any changes to it, will be tracked by hibernate
// and reflected to the database when the persistence context is flushed.
session.save(city);

detached

the entity has an associated identifier, but is no longer associated with a persistence context (usually because the persistence context was closed or the instance was evicted from the context)

分离的

实体有一个关联的标识符,但不再与持久化上下文相关联(通常是因为持久化上下文已关闭或实例已从上下文中逐出)

// city is in a detached state, Hibernate is no longer aware of the entity 
session.evict(city)

removed

the entity has an associated identifier and is associated with a persistence context, however it is scheduled for removal from the database.

移除

该实体有一个关联的标识符并与一个持久化上下文关联,但是它计划从数据库中删除。

session.remove(city);


Note:Hibernate API offers couples of methods to switch between entity states, and I think it's worth exploring a Hibernate Session class.


注意:Hibernate API 提供了几种在实体状态之间切换的方法,我认为值得探索一个Hibernate Session 类