There are three types of objects in Hibernate:
Temporary, free, and persistent, three methods of state conversion are called through the session.
What is persistence?
In layman's parlance, instantaneous data (such as in-memory data, which cannot be persisted permanently) persists into persistent data (for example, persistent to a database and can be persisted for a long time).
Two or three different states of mutual conversion
Session method
Session.save (): This method can convert an object from a temporary reload to a persistent state
Session.get (): Extracts an object from the database based on a primary key, which is a persisted state object
Session.update (): Turn an object into a persisted object
Session.evict (): Turns a persisted state object into a de-tube state
Session.clear (): Changes the persisted state of all hibernate objects to a de-state object
Three, the test session of the various methods
public class Sessionfatorytest {sessionfactory factory; Session session; Transaction Transaction; @Beforepublic void Init () {factory = Hibernateutils.getsessionfactory (); session = Factory.opensession (); transaction = Session.begintransaction ();} /** * Turn a temporary object into a persisted object */@Testpublic void Testsave () {User user = new User (); User.setage (one); User.setname ("AAAA"); Session.save (user);} @Testpublic void Testget () {User user = (user) Session.get (user.class, 1); SYSTEM.OUT.PRINTLN (user);} @Testpublic void Testupdate () {User user = (user) Session.get (user.class, 1); User.setage (102);//session.update (user); /Because the user itself is a persisted object, even if session.update () is not written, the SQL statement is issued at//transaction.commit () because the object's data is inconsistent with the snapshot (copy) of the object in//hibernate. When Flush () is issued, SQL} @Testpublic void TestUpdate2 () {User user = (user) Session.get (user.class, 1); session.evict (user);// Off-State user.setage (+); session.update (user);//Changed to persistent state} @Afterpublic void Destory () {transaction.commit (); Session.close (); Factory.close ();}}
Iv. a copy of the Hibernate object (snapshot)
Each time the Transaction.commit () is performed, the flush operation is performed to compare the data of the object to the copy and decide whether to send the SQL statement, for example, the user's age is 10 and changed to 10,user.setage (10); Then this will not send the SQL statement.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hibernate learning Notes (ii)-Explore the status of objects in Hibernate