您的位置:首页 > 业界新闻

Hibernate学习笔记:对象生命周期

2009-08-31 17:17 375 查看
学到这里我想谈一下自己的感受:在hibernate的世界里,要转换一下思维方式,那就是面向对象;在使用hibernate的时候,要忘记关系表,头脑中存在的就是

对象以及对象之间的关系.当配置文件编写完毕之后,对象和对象之间的关系也就确定了.由hibernate掌握所有对象的生命周期.
hibernate中的对象具有3种状态:
1. Transient Objects临时对象
Objects instantiated using the new operator aren't immediately persistent. Their state is transient, which means they aren't

associated with any database table row, and so their state is lost as soon as they're dereferenced.
新生成的对象, Session没有引用指向它, 没有放入session缓存中,它在数据库里没有相对应的数据.
2. Persist Objects持久化对象
A persistent instance is any instance with a database identity. Persistent instances are associated with the persistence manager.

Persistent instances are always associated with a Session and are transactional
放入session缓存中,Session有引用指向该对象, 它在数据库里有相对应的数据, 与数据库里的数据同步.
3. Detached Objects游离对象
Instances lose their association with the persistence manager when you close() the Session. We refer to these objects as detached,

indicating that their state is no longer guaranteed to be synchronized with database state; they're no longer under the management

of Hibernate.
已经被持久化,但不再处于session缓存中,Session已没有引用指向该对象, 数据库里可能还有相对应的数据, 但已不能与数据库里的数据同步.
Hibernate的那些行为可以影响对象的状态呢?下图有一个说明(该图来自互联网):


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐