4.1 Entity states

Hibernate 官方文档:Chapter 3. Persistence Contexts

4.1.1 临时对象(New or Transient):

  • 在使用代理主键的情况下, OID 通常为 null
  • 不处于 Session 的缓存中
  • 在数据库中没有对应的记录

new, or transient([‘trænzɪənt] adj. 短暂的;路过的) - the entity has just been instantiated([ɪn’stænʃɪeɪt] v. 实例化(instantiate的过去分词);具现化,实体化) and is not associated with a persistence context. It has no persistent representation in the database and no identifier value has been assigned.

4.1.2 持久化对象(Managed or Persistent):

  • OID 不为 null(has an associated identifier)
  • 位于 Session 缓存中
  • 若在数据库中已经有和其对应的记录(associated with a persistence context), 持久化对象和数据库中的相关记录对应
  • Session 在 flush 缓存时, 会根据持久化对象的属性变化, 来同步更新数据库
  • 在同一个 Session 实例的缓存中, 数据库表中的每条记录只对应唯一的持久化对象

managed, or persistent([pə’sɪst(ə)nt] adj. 固执的,坚持的;持久稳固的) - the entity has an associated identifier and is associated with a persistence context.

4.1.3 游离对象(Detached):

  • OID 不为 null
  • 不再处于 Session 缓存中
  • 一般情况需下, 游离对象是由持久化对象转变过来的, 因此在数据库中可能还存在与它对应的记录

detached([dɪ’tætʃt] adj. 分离的,分开的;超然的) - 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(evict [ɪ’vɪkt] vt. 驱逐;逐出) from the context)

4.1.4 删除对象(Removed):

  • 在数据库中没有和其 OID 对应的记录
  • 不再处于 Session 缓存中
  • 一般情况下, 应用程序不该再使用被删除的对象

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