Hibernate learning 5-hibernate Action Object 2

Source: Internet
Author: User

section II: Session Common Methods explained2) load and get () method:There is no record in the database corresponding to the OID, and the load () method throws an exception: the Load method defaults to lazy loading, and the Load object is a proxy class. There is no value at the beginning, only the use of its properties, and so on, will emit the SQL statement. The Get method emits an SQL statement at the outset. If you get an object to delete it, you can use load, because you just get a reference. If you say get an object to access its properties, it is recommended to use get;
@Test      Public void Testloadclass () {        = (Class) Session.load (Class).  Class, long.valueof (2));    // Class ID 2 does not exist, throws an exception         System.out.println (c.getstudents ());    }        @Test    publicvoid  Testgetclass () {        = (Class) Session.get (Class).  Class, long.valueof (2));    // a class ID of 2 does not exist, printing null         System.out.println (c);    }
View Code

2) Update:

@Test      Public void Testupdateclass () {        Session session1=sessionfactory.opensession ();        Session1.begintransaction ();        Class C= (Class) Session1.get (Class).  Class, long.valueof (1));        Session1.gettransaction (). commit ();         Session1.close ();                Session session2=sessionfactory.opensession ();        Session2.begintransaction ();        C.setname ("08 Computer undergraduate 2");        Session2.update (c);        Session2.gettransaction (). commit ();        Session2.close ();    }
View Code

Add:

Update Method: 1. Updating an detached object; 2. Update a transient error, but update the transient object with the ID set can (database has corresponding record); 3. Above, for example, update teacher, we just want to update name, but it will All properties are updated once, which results in low efficiency, such as having a field that is particularly long ... 4. Persistent object, as long as changes its contents, the session at the time of commit or close, will check the cache and the database is consistent, if not consistent, automatic UPDATE statement; But as with the above, although only one field is changed, All fields are also updated; 5. Can not do: Which field changed to update, which field has not changed, which field is not updated? What to do:a.xml configuration: <classname= "Com.cy.Teacher" dynamic-update= "true" >.....</class>B. Issues with cross-session updates:@Test public void TestUpdate5 () {Session session = Sessionfactory.getcurrentsession ();        Session.begintransaction ();        Student s = (Student) session.get (Student.class, 1);        S.setname ("Zhangsan5");                Session.gettransaction (). commit ();                S.setname ("Z4");        Session Session2 = Sessionfactory.getcurrentsession ();        Session2.begintransaction ();        Session2.update (s);    Session2.gettransaction (). commit (); First Student object is placed in the cache, S.setname ("Zhangsan3"), Hibernate will check which properties have been changed, this time generate SQL statement, because the use of dynamic-update, it only updated name this field. After the session is submitted, it is closed. The object in the cache is gone. But in memory student s This object is still in the detached state. This object again SetName ("Z4"), the second session2 came, the session2 there is no object of S, and then update (s), it has no place to compare which field has been changed? NoIt is not able to compare the s in memory with the s in the Session2 cache., so update (s), the issued SQL will update all fields; C. Based on above, if you want to cross the session, only update the changed fields, how to do: Change the above update toMerge: @Test public void TestUpdate6 () {Session session = Sessionfactory.getcurrentsession ();        Session.begintransaction ();        Student s = (Student) session.get (Student.class, 1);        S.setname ("Zhangsan6");                Session.gettransaction (). commit ();                S.setname ("Z4");        Session Session2 = Sessionfactory.getcurrentsession ();        Session2.begintransaction ();        Session2.merge (s);    Session2.gettransaction (). commit (); }merge: Merge This object into the database; Do you want to merge the original content? No need. Merge, how does it check which fields have changed which fields have not changed? Not in the cache, only one load from the database, so it's before updateA SELECT statement was issued first, and then compare the objects you gave me and the objects where I load are different, and then resend the UPDATE statement. D:dynamic-update This XML configuration, the corresponding JPA annotation does not have corresponding attributes, and it is recommended in real developmentHQL: Session.createquery ("Update Student s set s.name= ' Z5 ' where s.id = 1"); @Test public void TestUpdate7 () {Sessio        n session = Sessionfactory.getcurrentsession ();        Session.begintransaction ();        Query q = session.createquery ("Update Student s set s.name= ' Z5 ' where s.id = 1");        Q.executeupdate ();            Session.gettransaction (). commit (); }

3) Saveorupdate:

Saveorupdate (): If a temporary object is passed, the Save method is executed, and if a free object is passed, the Update method is called;
@Test Public voidTestsaveorupdateclass () {Session Session1=sessionfactory.opensession ();        Session1.begintransaction (); Class C= (Class) Session1.get (Class).class, long.valueof (1));        Session1.gettransaction (). commit ();                Session1.close (); Session Session2=sessionfactory.opensession ();        Session2.begintransaction (); C.setname ("08 Computer Undergraduate 3"); Class C2=NewClass (); C2.setname ("09 Computer Undergraduate 3");        Session2.saveorupdate (c); //c is Free State, perform updateSession2.saveorupdate (C2);//C2 Temporary state, execute savesession2.gettransaction (). commit ();                Session2.close (); /*** Issued sql: * Hibernate:select class0_.classid as classid1_0_0_, class0_.classname as classnam2_0_            0_ from T_class class0_ where class0_.classid=?            Hibernate:insert into T_class (className) VALUES (?) Hibernate:update T_class set classname=?         where classid=? */    }
View Code

4) Merge:

Sometimes update will error: There are two objects in the session, with the same OID (for example, OID is 1), update, session found in the cache you already have an OID is 1 objects, so update when the error;
  @Test  public  void   TestUpdateClass2 () {Session session1  =sessionfactory.opensession ();        Session1.begintransaction (); Class C  = (Class) Session1.get (Class.        Class , long.valueof (1 =sessionfactory.opensession ();        Session2.begintransaction (); Class C2  = (Class) Session2.get (Class., long.valueof (1 08 Computer undergraduate 3 "
View Code

Execution Error:

In order to solve this problem, more than one merge method, merging objects: When the update of the object OID and the session cache another object OID coincident, call the merge method will be merged, the properties of the two objects are merged, and then updated;
  @Test  public  void   Testmergeclass () {Session session1  =sessionfactory.opensession" ();        Session1.begintransaction (); Class C  = (Class) Session1.get (Class.        Class , long.valueof (1 =sessionfactory.opensession ();                Session2.begintransaction (); Class C2  = (Class) Session2.get (Class., long.valueof (1 08 Computer undergraduate 4 "
View Code

5) Delete:

@Test      Public void testdeletestudent () {        Student Student= (Student) session.load (Student.  Class, long.valueof (1));        Session.delete (student);        Session.gettransaction (). commit ();            Session.close ();    }
View Code

Because when you delete it, you just need to get a reference to it, which is done using load delay loading. You do not need to use GET, because you do not need to get the properties inside it.

Session.delete time has not really deleted, commit the transaction time, only synchronize the database, really deleted.

Hibernate learning 5-hibernate Action Object 2

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.