这篇文章是关于Hibernate的三个Life cycle state的总结。 了解清楚会对hibernate的工作原理有很好的认识。
any instance of object only can be three states:
- Transient
- persistent
- detached
Transient :
Hibernate not manage transient object.
In transient state, POJO is communication with the client code only, no any connection with database
Persistent
in Persistent state, a Session object created to manage the persistence of the Java object and database,
change of both side will keep up-to-date.
Detached
In Detached state, POJO still has a presentation in the database but the change of POJO will not make database updated, and vice versa.
it's created by session.close() or session.evict()
detached state can be back to persistence state by connecting to a new session. it happens when callingload()
,
refresh()
, merge()
, update()
, orsave()
methods on the new session with a reference to the detached object.