Hibernate learning Notes (ii)-Explore the status of objects in Hibernate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.