什么是休眠中的分离,持久和瞬态对象? [英] What are detached, persistent and transient objects in hibernate?
问题描述
一个持久性 new 没有与 Session
关联的类在数据库中没有表示,并且没有将标识符值视为 transient Hibernate:
Person person = new Person();
person.setName(Foobar);
//人处于过渡状态
A 持久 实例在数据库中具有一个表示形式,一个标识符值并与 Session
关联。您可以通过将 persistent 与
Long id =(Long)session.save(person);
//人现在处于持久状态
现在,如果我们 close
Hibernate Session
,持久化实例将成为 detached 实例:它不再附加到 Session
中(但是仍然可以修改并在稍后重新附加到新的 Session
中) 。
所有这些在整个第10章。处理Hibernate文档中的对象,我只是在上面进行了解释。当然,必须阅读。
What are detached, persistent and transient objects in hibernate? Please explain with an example.
A new
instance of a persistent class which is not associated with a Session
, has no representation in the database and no identifier value is considered transient by Hibernate:
Person person = new Person();
person.setName("Foobar");
// person is in a transient state
A persistent instance has a representation in the database, an identifier value and is associated with a Session
. You can make a transient instance persistent by associating it with a Session
:
Long id = (Long) session.save(person);
// person is now in a persistent state
Now, if we close
the Hibernate Session
, the persistent instance will become a detached instance: it isn't attached to a Session
anymore (but can still be modified and reattached to a new Session
later though).
All this is clearly explained in the whole Chapter 10. Working with objects of the Hibernate documentation that I'm only paraphrasing above. Definitely, a must-read.
这篇关于什么是休眠中的分离,持久和瞬态对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!