The newly created object is immediately deleted
@Testpublic void Testdelete () {Teacher t = new Teacher (); T.setname ("T1"); T.settitle ("Middle"); T.setbirthdate (New Date ( )); Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Session.save (t); System.out.println (T.getid ()); Session.gettransaction (). commit (); Session Session2 = Sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.delete (t); Session2.gettransaction (). commit ();}
Delete the object with the specified ID:
@Testpublic void TestDelete2 () {Teacher t = new Teacher (); T.setid (2); Session Session2 = Sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.delete (t); Session2.gettransaction (). commit ();}
The proxy object that is returned by load, and the SQL statement is emitted when the object is actually used.
Get is loaded directly from the database, issuing SQL statements without delay.
So load must perform the operation of the GetName method before commit.
But get put before commit can.
@Testpublic void TestLoad () {Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.load (Teacher.class, 1); This number is IDSystem.out.println (T.getname ()); Session.gettransaction (). commit ();//system.out.println (T.getclass ());} @Testpublic void Testget () {Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher.class, 1); Session.gettransaction (). commit ();//system.out.println ( T.getclass ()); System.out.println (T.getname ());}
Update
First case: Update the persistent object with the detached object.
@Testpublic void TestUpdate1 () {Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher.class, 1); Persistentsession.gettransaction (). commit (); T.setname ("Zhanglaoshi"); After commit, detachedsession Session2 = Sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.update (t); Persistentsession2.gettransaction (). commit ();}
Second case: Updating the Transient object will cause an error
@Testpublic void TestUpdate2 () {Teacher t = new Teacher (); T.setname ("Zhanglaoshi"); Session Session2 = Sessionfactory.getcurrentsession (); Session2.begintransaction (); session2.update (t); Session2.gettransaction (). commit ();}
Third case, manually set the ID after update, the database has a corresponding record
@Testpublic void TestUpdate3 () {Teacher t = new Teacher (); T.setid (3); T.setname ("Zhanglaoshi"); Session Session2 = Sessionfactory.getcurrentsession (); Session2.begintransaction (); session2.update (t); Session2.gettransaction (). commit ();}
Fourth situation: When we update, we do not set another field, but the console outputs all the fields and is inefficient.
An object with a persistent state will be updated whenever a different field is set:
@Test public void TestUpdate4 () {Session session = Sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher.class, 1); Persistent status T.setname ("Zhangsan2"); The cache and database contents are inconsistent, and the commit is Unified session.gettransaction (). commit (); }
Finally, we didn't get the effect we wanted: there are 3 ways to update only the fields that you want to change:
1. xml and annotation settings
Annotation method: Teacher.java do not want to update the field before the Get method, add @column (Updatable=false)
Xml method: <property name= "title" Update=false> </property>
2. Using the dynamic-update of XML,
3. Recommended! ! HQL (EJBQL)
Note: Student after update is an object, not a table name
@Test public void TestUpdate7 () {Session 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 (); }
Hibernate--session crud method, delete, load,get,update