in the Hibernate in , the Persistence object is then divided into three periods during operation. . these three periods and Session Cycle-related .
instantaneous, respectively . (Transient), Lasting too (persistent) and Free State (Detached)
instantaneous State
A java object that opens up memory by the new command . For example : Student stu= newstudent (); If there are no variables to quote him , then be JVM Recycling . instantaneous objects exist in memory in isolation, meaning that they are only carriers that carry information, and do not have any association with data in the database. the Save () and Saveorupdate () methods of the session can be used to correlate an instantaneous object with a database and insert the information carried by the instantaneous object into the database through a mapping made by the configuration file, which is a persistent object. Have the same ID identifier as the database record (hibernate automatically assigns it the ID)
Instantaneous features:
( 1) not associated with session instance
( 2) There are no records associated with instantaneous objects in the database
Persistent State
persistent instances have corresponding records in the database session and transction Associated sessin persistent object changes will not change the data immediately transcation terminate ,commit after persisted object before this is dirty object
Use find,get, Load and the Iterater and other methods to query the data , are persistent objects . If an instantaneous object is referenced by a persistent object , The object will also become a persistent object .
If you use Delete () method, it becomes an instantaneous object, and when a session executes close () or clear (), evict (), the persisted object becomes a managed object.
Lasting Features:
( 1) Associate with session instance
(2) have records associated with persistent objects in the database
code example: public void Testsave () {Session session =null; Transaction tx=null; try{session= hibernateutils.getsesion (); TX =session.begintransaction ();//transient instantaneous State user User= new User (), User.setname ("SDF"), User.setpassword ("hanhan23"); User.setcreatetime (new Date ());//persistent, Session reference, PO persistent session.save (user); Referenced to become persistent state//But for dirty objects user.setname ("LISD"); Tx.commit ();//}catch (Exception e) {e.printstacktrace (); Tx.rollback ();} Finally{hibernateutils.closesession (session);} deteached status closed after sesion}
The above example shows the transient state being converted to a persistent state object
Managed State
associated with a persistent object. when the session is closed, the object becomes managed. The reference to the managed object is still valid and the object can continue to be modified. If a managed object is re-associated to a new session, it will be turned into a persistent one again. Changes during the managed state will be persisted to the database.
managed state has a database identity ID, so it can be associated again with the persistence layer through methods such as update (), Saveorupdate (), and Lock ().
Hosting Features:
( 1) Essentially the same as instantaneous objects
( 2) Just more than the instantaneous object a database record identity value ID
summary :
recognize the ,hibernate session transitions between states are closely related to the life cycle of an object just created in memory is Free State session Save as persistent ( or by session reference ). once sesion Close or delete Beginning of learning also please comment
Hibernate persisted object state