The application creates an object through the new statement, and the lifecycle of the object begins. When no reference variable references it, the lifecycle of the object ends, the memory occupied by it can be recycled by the JVM garbage collector. For a Java object to be persisted, it can be in one of the following three States in its lifecycle:
(1) temporary state (transient): It was just created with the new statement and has not been persisted yet, and is not in the session cache. A Java object in the temporary state is called a temporary object.
(2) Persistent: it has been persisted and added to the session cache. A persistent Java object is called a persistent object.
(3) detached: it has been persisted but is no longer in the session cache. A Java object in the free state is called a free object.
Figure 1 shows the complete state transition of a Java object. The session-specific method triggers the conversion of a Java object from one state to another. As shown in figure 1, when a Java object is in a temporary or free state, its lifecycle ends as long as it is not referenced by any variable, the memory occupied by it can be recycled by the JVM garbage collector. When it is in the persistent state, it is always in the lifecycle because the session cache will reference it.
Features of temporary objects
Temporary objects have the following features:
(1) It is not in the session cache. It can be said that it is not associated with any session instance.
(2) There is no corresponding record in the database.
In the following cases, the Java object enters the temporary state:
(1) When a Java object has just been created through the new statement, it is in the temporary State and does not correspond to any records in the database.
(2) The Delete () method of the session can convert a persistent object or a free object to a temporary object. For a free object, the delete () method deletes the corresponding records from the database. For a persistent object, the delete () method deletes the corresponding records from the database, and delete it from the session cache.
Features of persistent objects
Persistent objects have the following features:
(1) stored in the cache of a session instance. It can be said that persistent objects are always associated with a session instance.
(2) The persistence object corresponds to the relevant records in the database.
(3) When the session clears the cache, it synchronously updates the database based on the attribute changes of the Persistent Object.
Many methods of session can trigger Java objects into the persistent state:
(1) The save () method of session converts a temporary object to a persistent object.
(2) The objects returned by the load () or get () method of the session are always in the persistent state.
(3) the list set returned by the find () method of the session stores all persistent objects.
(4) the update (), saveorupdate (), and lock () Methods of the session transform the free object into a persistent object. (Nate Note: According to hibernate reference, an exception is thrown when a persistent object is updated using update)
(5) When A Persistent Object is associated with a temporary object and cascade storage is allowed, the session will change the temporary object to a persistent object during cache cleaning.
Free Object Features
Free objects have the following features:
(1) It is no longer in the session cache. It can be said that the free object is not associated with the session.
(2) The free object is transformed from a persistent object, so there may be records corresponding to it in the database (the premise is that no other program has deleted this record ).
Similar to a temporary object, a free object is not associated with a session. Therefore, Hibernate does not guarantee that their attribute changes are synchronized with the database. The difference between a free object and a temporary object is that the former is transformed from a persistent object, so there may still be corresponding records in the database, while the latter has no corresponding records in the database.
Session:
(1) When the session close () method is called, the session cache is cleared, and all persistent objects in the cache become free objects. If no variables are referenced in the application to reference these free objects, they end the lifecycle.
(2) The session evict () method can delete a persistent object from the cache and change it to a free state. When the session cache stores a large number of persistent objects, it will consume a lot of memory space. To improve performance, you can consider calling the evict () method to delete some persistent objects from the cache. However, in most cases, the evict () method is not recommended, but the depth of the object graph should be controlled through the query language or explicit navigation.