hibernate--Examples of three states persistent demo

Source: Internet
Author: User

The previous blog introduced its own understanding of hibernate three states, this one is mainly through the demonstration of the case to further understand the three states.


Persistent Status:

At this point the state persistence object is included in the management of the session, and when the thing is committed, the data is synchronized with the database, and if there is updated data will produce an UPDATE statement.

Session=hibernateutils.getsession (); Tx=session.begintransaction ();//transient state, at which time the object is not included in session management User=new User (); User.setname ("Zhang San"); User.setpassword ("123"); User.setcreatetime (new Date ()); User.setexpiretime (new Date ());// Persistent State//persistent The object is synchronized with the database when the object's properties are changed//hibernate the cache (dirty data) is cleaned up. Session.save (user); User.setname ("John Doe"); Tx.commit ();} catch (Exception e) {e.printstacktrace (); if (tx!=null) {tx.rollback ();}} Finally{hibernateutils.closesession (session);  Close session is now datached status}

The resulting SQL statement:



Datached Status:

The state is not managed by the database at this time, but there are corresponding data in the database. Re-holding a session in this state commits the state back to the persistent state, and the data is updated in the database.

try {session=hibernateutils.getsession (); Tx=session.begintransaction ();//transient status User=new User (); user.setName ("Zhang San"); User.setpassword ("123"); User.setcreatetime (new Date ()); User.setexpiretime (new Date ());//persistent State// Objects in the persistent state are synchronized with the database when//hibernate cleans up the cache (dirty data) when the object's properties change. Session.save (user); User.setname ("John Doe"); Tx.commit ();} catch (Exception e) {e.printstacktrace (); if (tx!=null) {tx.rollback ();}} Finally{hibernateutils.closesession (session);} datached status, re-take a session to submit. User.setname ("Harry"); try {session=hibernateutils.getsession (); session.begintransaction ();// The Datached state object is re-incorporated into the session management//The Persistent state object Session.update (user); Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace (); Session.gettransaction (). rollback (); Finally{hibernateutils.closesession (session);}
the resulting SQL statement:



In summary, the persistent state will be included in the management of the session, regardless of whether the object is inserted or updated will be the final data synchronization to the database, and generate the corresponding SQL statement; When the session is closed in the datached State, If you re-open a session, the object will be re-incorporated into the session, and the session commit will submit the data and produce the corresponding update SQL statement.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hibernate--Examples of three states persistent demo

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.