Transitions between the three tenses of hibernate

Source: Internet
Author: User

Criteria for judging status

OID and Session Correlation

public class Hibernateutils {private static sessionfactory sessionfactory=null;static{//get config  Load Config file configuration configure = new configuration (). Configure (); sessionfactory = Configure.buildsessionfactory ();} Get sessionpublic static Session getsession () {Session session = Sessionfactory.opensession (); return session;}}

Instantaneous state

Transient state------Persistent State

Save and Saveorupdata operations

@Test//transient state converted to persistent public void test02 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction ();//In Free State  no OID has  nothing to do with session user User=new user ("zhangsan111", "123456789", " Zhangfei "," [email protected] "," 15029021367 ", New Date ()," M ", 1," 0 "), Session.save (user),//in persistent state  related  to session There is an OID   (transient state into a persistent state) ts.commit (); Session.close ();}

Transient state------Free State requires only one OID (the primary key generation policy is qualified so that we can only convert by persistent state)

@Test//transient state converted to Free State public void test02 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction ();//In Free State  no OID has  nothing to do with session user User=new user ("zhangsan111", "123456789", " Zhangfei "," [email protected] "," 15029021367 ", New Date ()," M ", 1," 0 "), Session.save (user),//in persistent state  related  to session There are OID   (transient state into persistent state) ts.commit (); Session.close (); System.out.println (User.getuid ());//In a Free State  with OID  session closed}

Persistent state

Persistent state----Transient delete operation

  

@Test//persistent state converted to instantaneous public void test03 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction (); User U=session.get (User.class, "402898a45fcec558015fcec55d5c0000");//In persistent state associated with the  session  has OID   ( Transient state into persistent state) System.out.println (U); session.delete (U); Ts.commit (); Session.close ();}

Persistent----Free State close (session closed) clear clears primary cache (session) evict purge and cache (specified cache)

@Test//persistent state converted to Free State public void test03 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction (); User U=session.get (User.class, "987E21997C134DC4B6538010D101BCEF");//In persistent state associated with the  session  has OID   ( Transient state into a persistent state) ts.commit (); Session.close (); SYSTEM.OUT.PRINTLN (U);//At this time the Free State  has OID  session closed}

Free State

Free State----Persistent state

@Test//Free State converted to persistent public void test04 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction (); User U=session.get (User.class, "12");//In the persistent state associated with the  session  has an OID   (transient state into a persistent state) ts.commit (); Session.close (); U.setusername ("123456");//Free State session Session1 = Hibernateutils.getsession ();//Open transaction Transaction TS1 = Session1.begintransaction (); session1.update (U);//Persistent state ts1.commit (); Session1.close ();}

Instantaneous state of Free State----

@Test//Free State converted to instantaneous public void test04 () {//Get connection Session session = Hibernateutils.getsession ();//open transaction Transaction TS = Session.begintransaction (); User U=session.get (User.class, "12");//In the persistent state associated with the  session  has an OID   (transient state into a persistent state) ts.commit (); Session.close (); U.setusername ("123456");//Free State session Session1 = Hibernateutils.getsession ();//Open transaction Transaction TS1 = Session1.begintransaction (); Session1.delete (U);//Transient state ts1.commit (); Session1.close ();}

  

Transitions between the three tenses of 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.