Time: 2017-1-19-23:00
--hibernate the state of a persisted class
1, a persisted class: an entity class that maps to a database.
2, hibernate to facilitate the management of persistent classes, the persistence class is divided into three states:
1) transient state: transient (temporary state)
2) Persistent state: Persistent
3) off-state: detached (off-line)
--status of three persisted objects
1, Three persisted object status
1) transient state
No persistent identity oid, not associated with hibernate session object, is considered instantaneous, Lost references are recycled by the JVM.
No persistent identity oid, not associated with Session.
2) Persistent State
Persistent Identity oid exists, is associated with the current session, and the associated session is not closed and the transaction is not Committed.
Persistent Identity oid exists, associated with Session.
persisted objects have the ability to automatically update the Database.
3) off-state
Persistent OID exists, however, It is not associated with the current session and hibernate cannot be detected when the Off-tube state Changes.
Persistent Identity oid exists, not associated with Session.
2, distinguish the status of three persisted objects
Sample code:
public class HibernateDemo1 {@Test/* * Distinguishes three states of persisted objects: */public void fun1 () {//1, Create session Session session = Hibernateutils.opensession (); 2. Open transaction Transaction tx = Session.begintransaction (); /* * 3, logic operation */
Save a book to the database books = new (); Transient state: no unique identifier oid, no associated with session Book.setname ("Hibernate"); Book.setauthor ("zhang san"); Book.setprice (50D); Session.save (book); Book becomes a persistent state object with a unique identifier oid, associated with the session//4, commit transaction Tx.commit (); 5, Cast Resources Session.close (); Book.setname ("Struts2"); At this time the book is out of the pipe state, there is a unique identifier oid, but no association with the session}}
3. Conversion of three state objects
Instantaneous state:
Book book = new book ();
1) instantaneous State-to-last State
Book book = new book ();
Session.save (book);
2) instantaneous State---off-tube State
Book.setid (1); Set OID
Persistent state:
Book book = (book) session.get (book.class, 1);
Get ()/load ()/find () iterate ()
1) Persistent state---instantaneous State
Session.delete (book); After the deletion, there will be no oid.
2) Persistent---off-tube State
Session.close ()
Clear ()/evict Deletes the specified object in the Session.
Off-pipe state:
Book book = new book ();
Book.setid (1);
1) Off-the-long
Session.update (book);
2) off--instantaneous
Book.setid (null);
--persistent object updates the database automatically
Example Code:
/* * Persistent Object updates database automatically */public void fun2 () {session session = Hibernateutils.opensession (); Transaction tx = Session.begintransaction (); Get a persistent state object book book = (book) session.load (book.class, 1); Book.setname ("Struts2"); Tx.commit (); When submitted, an UPDATE statement is sent to automatically update the database data, relying on Hibernate's first-level cache Session.close (); }
--summary
1. Hibernate persists the state of the Object:
* Instantaneous state
The OID is not uniquely identified and is not associated with the session
* Persistent State
Has a uniquely identified oid, associated with the session
* Off-pipe State
Has a uniquely identified oid, not associated with the session
2. Instantaneous state object
User user = new user ()
Transient state---persistent State
Session.save (user)
Instantaneous State---off-tube State
User.setid (1)
3. Persistent State Object
Get ()/load ()
Persistent State--instantaneous State
Session.delete (Object)
Persistent---off-tube State
Session.close ()/session.clear ()/session.evice ()
4. Off-pipe State
User user = new user ();
User.setid (1)
Off-tube state--instantaneous State
User.setid (null)
Off-state---persistent State
Save ()/saveorupdate ()
Hibernate Persistent Objects