HibernateStatus of the Java object in the Application
When the applicationProgramAn object is created using the new statement, and the lifecycle of the object begins. When no reference points to it, the lifecycle of the object ends, the memory it occupies can be recycled by the JVM garbage collector.
For Java objects that need to be persisted, in its lifecycle. It can be in one of the following three States:
- Transient (temporary) transient:Newly created, not persistent, not in session cache. A Java object in the temporary state is called a temporary object (no data or session)
- Persistent:It has been persisted and added to the session cache. A persistent Java object is called a Persistent Object (with data and session)
- 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 (with data and no session)
ProgramCode |
PersonObject Lifecycle |
PersonObject status |
Tx = session. begintransaction (); Person = new person ("James "); |
Start Lifecycle |
Temporary status |
Session. Save (person ); |
In the lifecycle |
Change to persistent state |
Long id = person. GETID (); Person = NULL; Person person2 = (person) Session. Load (person. Class, ID ); TX. Commit (); |
In the lifecycle |
In the persistent state |
Session. Close () |
In the lifecycle |
Change to free state |
System. Out. println (person2.getname ()); |
In the lifecycle |
In the free status |
Person2 = NULL; |
End Lifecycle |
End Lifecycle |
Temporary status features:
- It is not in the session cache. It can also be said that it is not associated with any session instance.
- No corresponding records in the database
Under the following circumstances, the Java object enters the temporary state
- When a Java object is created using the new statement, it is in the temporary State and does not correspond to any records in the database.
- SessionThe Delete () method of 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.
Persistence object features:
- Stored in the cache of a session instance. It can be said that a persistent object is always associated with a session instance.
- The persistence object corresponds to the relevant records in the database
- SessionDuring cache cleanup, the database will be synchronously updated based on the attribute changes of the Persistent Object.
SessionMany methods can trigger Java objects into the persistent state.
- SessionThe SAVE () method of converts a temporary object to a persistent object.
- SessionThe objects returned by the load () or get () method of are always in the persistent state.
- SessionThe list set returned by the list () method stores all persistent objects.
- SessionUpdate () and saveorupdate () to convert a free object to a persistent object.
- When a persistent object is associated with a temporary object, when cascade storage is allowed, the session will change the temporary object to a persistent object when clearing the cache.
Features of free objects:
- It is no longer in the session cache. It can be said that the free object is not associated with the session.
- The free object is changed 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)