(1) Transient: The free presence in memory is independent of the records in the database. Java code public void MethodA {TUser user = new TUser (); User.setname ("Emma"); ---User is not associated with records in the database. }
public void MethodA {
TUser user = new TUser ();
User.setname ("Emma"); ---User is not associated with records in the database.
(2) Persistent: In the state managed by the Hibernate framework. References to entity objects are managed in the Hibernate entity container. Java code TUser user =NewTUser (); TUser Anotheruser =New tuser (); user.setname ("Emma"); anotheruser.setname ("Kevin" ; //At this time the user and anotheruser are in transient state transaction tx = session.begintransaction (); session.save (user); // The user object at this time has been included in the Entity Management container by Hibernate, in persistent state /and Anotheruser is still in transient state. tx.commit (); //transaction submitted, a user "Emma" has been inserted into the library table // For Anotheruser, there is no operation transaction tx2 = session.begintransaction (); user.setname ("emma_1"); //persistent anotheruser.setname ("kevin_1"); //Transient tx2.commit ();
TUser user = new TUser ();
TUser anotheruser = new TUser ();
User.setname ("Emma");
Anotheruser.setname ("Kevin");
At this point both the user and anotheruser are in the transient state
Transaction tx = Session.begintransaction ();
Session.save (user);
The user object at this time has been included in the Entity Management container by Hibernate, in persistent state
//And Anotheruser is still in transient state.
tx.commit ();
After a transaction is committed, a record of the user "Emma" has been inserted into the library table
//There is no operation for anotheruser
Transaction tx2 = session.begintransaction ();
User.setname ("emma_1"); Persistent
anotheruser.setname ("kevin_1");//transient
tx2.commit ();
Although we did not explicitly invoke the Session.save () method to save the user object in this transaction, it is
Objects in the persistent state are automatically solidified into the database, so changes to the user object will also be synchronized to the
database, which means that the user record for "Emma" in the database has been updated to "emma_1"
At this point anotheruser is still a normal Java object, in the transient state, it is not subject to hibernate
Framework management, so changes to its properties do not have any effect on the database.
Transient------session.save ()------> Persistent
Object------session.load ()------> Persistent
(Object---session (valid)-----> Persistent) Java code//persistent object returned by hibernate TUser user = (TUser) session.load (TUser. class,new Integer (1)); In the Session.load method, before returning an object, Hibernate has included its object in//its entity container
The persistent object returned by hibernate
TUser user = (TUser) session.load (tuser.class,new Integer (1));
Session.load method, before returning an object, Hibernate has included its object in the
Persistent object <------one by one corresponding------> a record in the database
(3) Detached:
OBERCT (persistent)-----The corresponding session instance closes-----> Object (Detached) Java code TUser user = new TUser (); User.setname ("Emma");//user in transistent Transaction tx = Session.begintransaction (); Session.save (user);//user in persistent tx.commit (); Session.close ();//user in Detached
TUser user = new TUser ();
User.setname ("Emma");//user in transistent
Transaction tx = Session.begintransaction ();
Session.save (user);//user in persistent
tx.commit ();
&NBSP
Detached differs from transient: The
Detached object can again be associated with a session instance and become a persistent object (transient can also, But it's not the same. User---Transient state lacks a corresponding relationship to the data in the library table, whereas the user in the Deatached state has a record in the library table (uniquely determined by the primary key). The
artificially manufactures a detached object: Java code tuser user = new tuser (); User.setname ("Emma"); //hard-coded to specify a primary key value (assuming that there are id=1 records in the library table) User.setid ( New integer (1)); //At this time user becomes a "man-made detached object" Transaction tx = session.begintransaction (); session.update (user); The session changes its primary key value to persistent user.setage ( new integer); Tx.commit (); session.flush (); session.close ();
TUser user = new TUser ();
User.setname ("Emma");
Hard-coded to specify the primary key value (assuming that there are id=1 records in the library table)
User.setid (New Integer (1));
At this time the user becomes a "man-made detached object"
Transaction tx = Session.begintransaction ();
Session.update (user);//session changes it to persistent
User.setage (new Integer) based on its primary key value;
Tx.commit ();
Session.flush ();
Object (persistent)---session.delete ()--->object (Transient)
Transient: The corresponding record could not be found from the library table.
Detached: The corresponding record can be found from the library table (just not associated with the session).
In general, you should avoid passing the PO directly to other layers of the system by constructing a new VO, which has the same property value as the PO by the property parent indicator, and as the transport medium (actually, this VO is used as the data Transfer Object, or a dto), Pass this VO to another level to achieve the required data transfer.