I read about Transient, Persistent and Detached. It seems most part is straight forward to understand. What I am not able to get over is, when we use session.close()
the entity objects that are already in first level cache gets deleted or cleared and in my view there is no chance to bring or put back the entity or object to persistent state. But then I see or read from different source is that, detached entity can still be reattached to a new session at later though. How is it possible when first level cache is cleared after doing session close?
How is it possible for the detached entity to be a persistent object in hibernate?
213 views Asked by MKod At
1
I'd like to assure you, that this is not so complicated. Simply...
session
(session instance) - and close suchsession
while keep reference to loaded entity. Thatentity
will become detached, once thesession
is closedsession
(different session instance) - and re-attach that entity. We are then able to persist changes made meanwhile.That's it. More details from doc:
11.6. Modifying detached objects
So, the point here is ... the entity loaded with
session
is not transient. It is persisted already. It becomes detached once session is closed. But could be re-atteched later, to another session