Catalog:(Follow-up update ...) )
- Getting Started with Hibernate 01-Framework technology (Introduction to the development of the Hibernate framework)
- Hibernate Introductory 02-hibernate Introduction and its environment construction
- Getting Started with Hibernate 03-use Hibernate to complete the persistence operation
- Hibernate entry 04-hibernate Three states of Java objects
Three states of Java objects in ▁▃▅hibernate ▅▃▁
1. Three states of Java objects
When an app interacts with the framework by calling the Hibernate API, it needs to focus on the life cycle of the Application object from a persistence perspective.
The persistence declaration cycle is a key concept in Hibernate, and the correct understanding of the life cycle provides a better understanding of Hibernate's implementation principles and the correct usage of hibernate.
The Hibernate framework manages the state of Java objects through the session, and in the persistence lifecycle, there are three states of Java objects:
- Instantaneous state (Transient)
After creating an object from new, the object does not have a like persistence, and it is not associated with the data in the database, when the state of the Java object is instantaneous.
The session is ignorant of the instantaneous state of the Java object, and when the object is no longer referenced by another object, all its data is lost, and the object will be processed by the Java Virtual Machine in accordance with the garbage collection mechanism.
- Persistent State (persistent)
When the object is associated with the session and is managed by the session, it is in a persistent state. An object that is in a persistent state has a database identity (a primary key value in the database).
So, when is the object associated with the session? There are two ways of doing this:
The first, when loading an object from a database by Sesison's query interface, or the Get () method, or the load () method, is associated with a record in the database table, when the object is associated with the session that loaded it;
The second, instantaneous state of the object, through the session's Save () method or the Saveorupdate () method, the Java object is also associated with the session.
For objects that are in a persistent state, the session continuously tracks and manages them, and if any changes are made to the internal state of the object, hibernate chooses the appropriate time (such as when the transaction commits) to solidify the changes into the database.
Objects that are in a persistent state are in a free state after they are detached from the management of the nsession associated with them.
In a free state, the session cannot guarantee that the data contained in the object is kept from the records in the database, because hibernate is already unable to perceive any action on the object.
The session provides two methods (update (), merge ()), and associates the object in a free State with a new session.
At this point, the state of the object is re-converted from a Free State to a persistent state.
2. Conversion between three states
In hibernate applications, different persistence operations can cause changes in the state of the object. Describes the transformation of an object's state:
Use the New keyword to build an object that has a state of instantaneous state.
2.1 Transient status to persistent state
When an object is saved using the Save () or Saveorupdate () method of the Session object, the state of the object is converted from the transient state to a persistent state.
Gets the object using the Get () or load () method of the Session object, which has a persistent state.
2.2 Persistent state to instantaneous state
After the Delete () method of the Session object is executed, the object changes from the original persistent state to the instantaneous state because the object is not associated with any database data at this time.
2.3 Persistent state to Free State
Eat the evict (), clear (), or Close () methods of the Session object, and the object is moved from the previous persistent state to the Free State.
2.4 Free State to persistent state
Retrieves the session object, executes the update () or Saveorupdate () method of the Session object, and the object is moved from the Free State to the persistent state, which is associated with the session object again.
2.5 Free State transitions to instantaneous state
Executes the Delete () method of the Session object, and the object is shifted from the Free State to the instantaneous state.
When an object that is in a transient or Free State is no longer referenced by another object, it is processed by the Java virtual machine as a garbage collection mechanism.
3. Practice (Better memory than bad keyboard)-the state of the output object
Requirements: Supplement the statements for the following two paragraphs to output the state of each stage object.
Code Listing 1:
1 Try{2 //omit part of the code ...3Session =sessionfactory.opensession ();4tx = Session.begintransaction ();//Start a transaction5User user = (user) session.load (user).class,NewInteger ("1001"));//get User Object6User.setusername ("Zhang Dong");//Modify user Information7Tx.commit ();//Commit a transaction8}Catch(hibernateexception e) {9 e.printstacktrace ();TenTx.rollback ();//rolling back a transaction One}finally{ A if(NULL!=session) { -Session.close ();//Close Session - } the}
Code Listing 2:
1 Try{2 //omit part of the code3Session = Sessionfactory.opensession ();//Open Session4tx = Session.begintransaction ();//Start a transaction5User user = (user) session.load (user).class,NewInteger ("1000"));//get the User object6Session.delete (user);//Persistent operation-delete7Tx.commit ();//Commit a transaction8}Catch(hibernateexception e) {9 e.printstacktrace ();TenTx.rollback ();//rolling back a transaction One}finally{ A if(NULL!=session) { -Session.close ();//Close Session - } the}
Hibernate entry 04-hibernate Three states of Java objects