Public static void addstudent () { sessionfactory= New annotationconfiguration (). Configure (). buildsessionfactory (); session session=sessionfactory.getcurrentsession (); session.begintransaction (); student student=new student ( "john doe", 34); session.save (Student); student.setname ("harry"); iterator<student> iterator= (iterator<student>) session.createquery ("from student "). Iterate (); while ( Iterator.hasnext ()) { system.out.println (iterator.next (). toString ()); } session.gettransaction (). Commit ( ); }
Attention
Session.save (student); Student.setname ("harry");
After persisting the entity, the session is only written to the session cache, and the commit is not written to the database at this time and then changes
entity, the entity saved to the database is a modified Entity.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8A/B5/wKiom1g4CVqxPk7FAAAU70Hay9U234.png-wh_500x0-wm_3 -wmp_4-s_1205555195.png "title=" Capture. PNG "alt=" wkiom1g4cvqxpk7faaau70hay9u234.png-wh_50 "/>
Public static void updatestudent () { Sessionfactory=new annotationconfiguration (). Configure (). buildsessionfactory (); session session=sessionfactory.getcurrentsession (); session.begintransaction (); student student= (student) session.get (student.class, 2); Student.setname ("update"); iterator<student> iterator= (iterator<student>) session.createquery ("from student"). Iterate (); while (iterator.hasnext ()) { system.out.println (iterator.next (). toString ( )); } session.gettransaction (). Commit (); }
After looking for a record with ID 2 from the database, the session cache has an entity that modifies the Entity's value directly, and the Database's records are still modified without update,session commit.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8A/B1/wKioL1g4C0fDd1pjAAAT5rdZOLg644.png-wh_500x0-wm_3 -wmp_4-s_1293114855.png "title=" Capture. PNG "alt=" wkiol1g4c0fdd1pjaaat5rdzolg644.png-wh_50 "/>
For the first query and then modify,
Do not write Update,merge and
Session.update (student);
Session.merge (student);
All the same, the data will be written to the database after Commit.
Student student=new Student ("update", 100); Session.update (student);
Directly update an entity (its id=10 record does not exist in the database), and the error is directly
Org.hibernate.StaleStateException:Batch Update returned unexpected row count from update [0]; Actual row count:0; Expected:1
The ID of this student object is set to self-increment, and its id=10 record does not exist in the Database.
Student student=new Student ("update", 100); Session.merge (student);
The result database holds the record, but the ID is not 10, but it is 6 (the maximum is 5 before insertion), as a result of the self-increment
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8A/B5/wKiom1g4EJzgXwNPAAAQnrdLgNs412.png-wh_500x0-wm_3 -wmp_4-s_397457984.png "title=" Capture. PNG "alt=" wkiom1g4ejzgxwnpaaaqnrdlgns412.png-wh_50 "/>
Student student=new Student (one, "update", 100); Session.saveorupdate (student);
and Saveorupdate (), which can do this in the imagination, has an Error.
Org.hibernate.StaleStateException:Batch Update returned unexpected row count from update [0]; Actual row count:0; Expected:1
Hibernate:update Student Set age=?, name=? where id=?
Sent SQL unexpectedly is update, How is not save, should be automatic selection is right?
This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1876692
Hibernate uses save to persist the entity before changing the value of the entity