Common methods in the 02hibernate-03session of Java combat

Source: Internet
Author: User

Ix. Common methods in the Session 1.Save method

It's all temporary. ————> Persistent State

2.persist method

Role:

Persists the temporary state object.

Differences from the save method:

Started the transaction:persist and save are no different.

Do not open transaction:

Persist: Nothing is going to be done.

Save:

Hibernate3: plan to save the data because the transaction is not turned on and automatically rolled back.

Hibernate5: Provides a built-in transaction to perform a save operation.

1 /*2 * Save method and persist method3 * Common denominator:4 * It's all about turning the temporary state object into a persistent state .5 * Difference:6 * 1, the provider is not the same7 * The Save method is provided by Hibernate8 * The Persist method is the Java persistence API provided by the JPA specification9 * 2, without the support of the businessTen * Save method: One * Hibernate5: The save operation is performed with a transaction built into the framework.  A * Hibernate3: The save is executed, but there is no explicit commit operation, and after the timeout, the transaction is automatically rolled back.  - * Persist method: - * Nothing to do.  the      *       -      */ - @Test -      Public voidtest2 () { +Student S1 =NewStudent ();//Temporary State -S1.setname ("Test3"); +S1.setgender ("female"); AS1.setbirthday (NewDate ()); at          -          -Session s =hibernateutil.getsession (); -         //Transaction tx = S.begintransaction (); -S.persist (S1);//Persistent State -         //tx.commit (); in s.close (); -     } to      + @Test -      Public voidtest1 () { theStudent S1 =NewStudent ();//temporary state: No OID, no relation to session *S1.setname ("Test4"); $S1.setgender ("Male");Panax NotoginsengS1.setbirthday (NewDate ()); -          the          +Session s =hibernateutil.getsession (); ATransaction tx =s.begintransaction (); theS.save (S1);//Persistent State: There is an OID, which is related to the session + tx.commit (); - s.close (); $}
3 ,update method

take off the tube state --------> Persistent State

The UPDATE statement isalso executed when the properties of the 3.1 and the off-state object have not changed . Can be in <class select-before-update= "true" > change. This configuration can be done in development for entities that do not frequently change properties

Solve the problem:

3.2,update an out-of-state object, if there is already an OID Same Persistent state object, then the error

3.3,update a de-state object, the database does not have a corresponding record (deleted by another program), also wrong

/** Update method * Convert an off-state object to a persistent state*/    /** Question 3: * When we persist a off-state object, if the object is deleted by another program, it will be an error*/@Test Public voidTest5 () {Session s2=hibernateutil.getsession (); Transaction TX2=s2.begintransaction (); Student Student2= S2.get (Student.class, 1);//Persistent StateStudent2.setgender ("female"); S2.update (Student2);//to turn the off-state object into a persistent stateTx2.commit ();    S2.close (); }        /** Question 2: * When we persist a off-state object, an error will be made if an object with the same OID is already included in the first-level cache of the session. * Workaround: * Use the Merge method*/@Test Public voidtest4 () {Session S1=hibernateutil.getsession (); Transaction tx1=s1.begintransaction (); Student Studnet1= S1.get (Student.class, 1);//Persistent StateTx1.commit ();                S1.close (); System.out.println (STUDNET1);//off-State: There is an OID, and the session is not relatedSession S2=hibernateutil.getsession (); Transaction TX2=s2.begintransaction (); Student Student2= S2.get (Student.class, 1);//Persistent StateS2.update (STUDNET1);//to turn the off-state object into a persistent stateTx2.commit ();    S2.close (); }                /** Question 1: * When we use update to persist a de-configured object, the UPDATE statement is executed even if the data for the entity object is not modified. * Workaround: * select-before-update * Config location: Map file on class element * <class name= "Student" table= "Student" Selec T-before-update= "true" > * Value: * True: Execute query before update * false: Do not execute query before update. Default Value*/@Test Public voidtest3 () {Session S1=hibernateutil.getsession (); Transaction tx1=s1.begintransaction (); Student s= S1.get (Student.class, 1);//Persistent StateTx1.commit ();                S1.close (); System.out.println (s);//off-State: There is an OID, and the session is not relatedSession S2=hibernateutil.getsession (); Transaction TX2=s2.begintransaction (); S2.update (s);//Persistent StateTx2.commit ();    S2.close (); }
4.saveorupdate method

Function: Save or modify

Transient ————> persistence: Saving

Off-pipe ————> Persistent State: Update

Determine whether the save or update is based on the state of the object

Special cases:

When Unsaved-value> is set in <id in the object's mapping XML file, and when the OID remains consistent, The object is no longer considered a free State but a temporary state.

1 /*2 * Saveorupdate3 * Function:4 * Perform save or update5 * Execution basis:6 * Based on the state of the object. 7 * If the object is a temporary state, execute the Save8 * If the object is off-state, the update is performed9      */Ten @Test One      Public voidTest8 () { AStudent S1 =NewStudent ();//temporary state: No OID, no relation to session -S1.setname ("Test5"); -S1.setgender ("Male"); theS1.setbirthday (NewDate ()); -Session s =hibernateutil.getsession (); -Transaction tx =s.begintransaction (); -S.saveorupdate (S1);//Persistent State: There is an OID, which is related to the session + tx.commit (); - s.close (); +     } A @Test at      Public voidtest7 () { -Session s2 =hibernateutil.getsession (); -Transaction TX2 =s2.begintransaction (); -Student Student2 = S2.get (Student.class, 2);//Persistent State -Student2.setgender ("Male"); -S2.saveorupdate (Student2);//to turn the off-state object into a persistent state in tx2.commit (); - s2.close (); to}
5 ,Merge method

When you update a free object, an error occurs if there is already a persistent state object of the OID same. You can use merge Merge to copy the contents of the free object into the properties of an object like an OID in memory

1 /*2 * Merge Method:3 * Function: Merge two objects. Replace the data of the persisted object with the data of the off-state object. 4      */5 @Test6      Public voidTest6 () {7Session S1 =hibernateutil.getsession ();8Transaction TX1 =s1.begintransaction ();9Student Studnet1 = S1.get (Student.class, 2);//Persistent StateTen tx1.commit (); One s1.close (); A          -System.out.println (STUDNET1);//off-State: There is an OID, and the session is not related -          theSession s2 =hibernateutil.getsession (); -Transaction TX2 =s2.begintransaction (); -Student Student2 = S2.get (Student.class, 2);//Persistent State -S2.merge (STUDNET1);//to turn the off-state object into a persistent state + tx2.commit (); - s2.close (); +}

Common methods in the 02hibernate-03session of Java combat

Related Article

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.