From the perspective of the persistence layer, a java object can be in one of the following four states in its lifecycle:
Temporary status (transient): It was just created with the new statement and has not been persisted and is not in the Session cache.
Persistent: it has been persisted and added to the Session cache.
Removed: it is no longer in the Session cache. The Session has been scheduled to be deleted from the database.
Detached: it has been persisted but is no longer in the Session cache.
The code is clearer:
Code
Tx = session. beginTransaction (); start the temporary state of the lifecycle Student s1 = new Student ("tom", new HashSet (); session. save (s1); Long id = s1.getId () in the life cycle transition to persistent State; s1 = null in the life cycle; Student s2 = (Student) session. get (Student. class, id); tx. commit (); session. close (); the System is in the free status during the life cycle. out. println (s2.getName (); in the Free State of Life Cycle s2 = null; End of Life Cycle
The difference between a free object and a deleted object is that the free object and the Session are completely separated. The Session plans to delete the deleted object from the database. When the Session clears the h cache, the corresponding SQL delete statement is executed to delete the corresponding records from the database.