-Temporary state: The object has just been created with the new statement and is not persisted and is not in the session cache. A Java object that is in a temporary state is called a temporary object.
-Persistent state: has been persisted and added to the session's cache. A Java object in the persisted state becomes a persisted object.
-Free State: has been persisted, but is no longer in the session cache. A Java object that is in a free state becomes a free object.
//the process of object state transitionsTransaction ts=session.begintransaction (); User User=NewUser ();//Temporary statusUser.setname ("Tom"); User.setpassword ("123456"); Session.save (user); //transition to persistent stateTs.commit ();//Persistent StateSession.close (); //turn into a Free StateSessionfactory.close (); System.out.println (User.getname ()); //Free State
Characteristics of a temporary object: -oid is null -not in the session's cache, or it can be said that it is not associated with any one session instance -there is no corresponding record of the persisted object's characteristics in the database: -oid is not null -in the cache of a session instance, the persisted object is always associated with a session instance -The persisted object corresponds to the related record in the database -the database is updated synchronously based on the properties of the persisted object. user user= (user) Session.get (user.class,1);//Gets Persistent object persistent state user.setname ("Jerry"); Transaction.commit (); We found that we did not execute the UPDATE statement, but we printed the UPDATE statement. Hibernate automatically synchronizes the persisted object's state to the database. Characteristics of the Free object: -oid is not null -no longer in the session cache, it can be said that the free object is not associated with the session
Conversion of three states
1) Temporary state transition Persistence State-session the Save () method converts the staging state to a persistent state. Put the object you want to save into the session cache, and put it into a persistent state. Assigns a unique OID to a persisted object using the primary key generation policy specified by the mapping file. The Save method simply assigns the UID to the object. We can break the point at the Save method. When our primary key generation strategy is native, because we use MySQL database, the primary key is self-increment, so after executing the Save method, print the INSERT statement, MySQL database for our object auto-increment when our primary key generation policy is Incrementt, incre ment is maintained by hibernate, first go to the table to check the maximum ID then +1, after we execute the Save method, we find that the print select find the maximum ID of the statement,
Insert statement is printed when commit is executed 2) temporary state transitions to free State-the object OID of the temporary state is set to the corresponding record in the database. User User=new user (); User.setid (1); 3) The persistence state transitions to a temporary state first: User user= (user) Session.get (user.class,1);//Get Persisted Object Persistent state session.close () ; Free State user.setid (NULL);//Temporary state second: User user= (user) Session.get (user.class,1);//Get persisted object persistence State session.evict (user); Free State, this method clears the session cache to the persisted object, making it a Free State user.setid (NULL);//Temporary state 4) The persistence state transitions to the Free State first: Call the session's Close method, Persistent state becomes Free State second: Call the session's evict () method, turn the persistent state to Free State 5) The Free State is converted to a temporary state, only the object OID of the Free State is changed to NULL. 6) The Free State is converted to a persistent state session by the update () method to convert the Free State to a persistent state. User user= (user) Session.get (user.class,1);//Get Persisted Object Persistent state session.evict (user); Free State, this method clears the persistent object from the session cache and makes it a Free State session.update (user);
The state of the Java object in Hibernate persistence layer