JPA的实体有以下4中生命周期状态:
(1).New:瞬时对象,尚未有id,还未和Persistence Context建立关联的对象。
(2).Managed:持久化受管对象,有id值,已经和Persistence Context建立了关联的对象。
(3).Datached:游离态离线对象,有id值,但没有和Persistence Context建立关联的对象。
(4).Removed:删除的对象,有id值,尚且和Persistence Context有关联,但是已经准备好从数据库中删除。
四种状态总结:
状态名 作为Java对象存在 在实体管理器中存在 在数据库存在
New yes no no
Managed yes yes yes
Detached no no no
Removed yes yes no
四中状态的转化: