version (release number)
The version number configuration must indicate optimistic-lock= "version"
<?xml version= "1.0" encoding= "Utf-8"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hiber Nate-mapping-3.0.dtd ">
After you modify the student table in the test class Hibernatetest session1 session query, if the student table is modified in the Session2 session, The org.hibernate.StaleObjectStateException exception is thrown at this point, meaning that the value of version in the table will change from 0 to 1 (the initial value of version in the table is 0), and when the second attempt is made, the underlying will get the version of the query when it is modified. (a value of 0) compares to version 1 of the updated value in the database and finds that 0 < 1 throws the above exception (presumably the meaning of an expired class state exception).
The Hibernatetest class code is as follows:
Package com.hibernate;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
public class Hibernatetest {private static sessionfactory sessionfactory;
static {try {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
} catch (Exception ex) {ex.printstacktrace ();
}} public static void Main (string[] args) {Session session = Sessionfactory.opensession ();
Transaction tx = NULL; try {tx = Session.begintransaction ();//Student Student = new Student ();//Student.setname ("Zhangsan");//S
Tudent.setage (20);
Student.setcardid ("14");
Session.save (student);
Tx.commit ();
Session Session1 = Sessionfactory.opensession ();
Session Session2 = Sessionfactory.opensession (); Student student1 = (Student) session1.createquery ("from Student s where s.name =: Name"). SetString ("name", "Zhangsan").
Uniqueresult (); StudenT student2 = (Student) session2.createquery ("from Student s where s.name =: Name"). SetString ("name", "Zhangsan").
Uniqueresult ();
System.out.println (Student1.getversion ());
System.out.println (Student2.getversion ());
Transaction tx1 = Session1.begintransaction ();
Student1.setname ("Lisi");
Tx1.commit ();
System.out.println (Student1.getversion ());
System.out.println (Student2.getversion ());
Transaction tx2 = Session2.begintransaction ();
Student2.setcardid ("111");
Tx2.commit ();
System.out.println (Student1.getversion ());
System.out.println (Student2.getversion ());
} catch (Exception ex) {ex.printstacktrace ();
if (null! = tx) {tx.rollback ();
}} finally {Session.close ();
}
}
}
The output information for the corresponding console is as follows:
Hibernate:select student0_.id as id0_, student0_.version as version0_, Student0_.cardid as C
ardid0_, student0_.age as age0_, Student0_.name as name0_ from student student0_ where
Student0_.name=? Hibernate:select student0_.id as id0_, student0_.version as version0_, Student0_.cardid as C
ardid0_, student0_.age as age0_, Student0_.name as name0_ from student student0_ where
Student0_.name=?
0 0 hibernate:update Student set version=?, cardid=?, age=?, Name=?
where id=?
and version=?
1 0 hibernate:update student set version=?, cardid=?, age=?, Name=?
where id=?
and version=? <span style= "color: #ff0000;" >org.hibernate.staleobjectstateexception:row is updated or deleted by another transaction (or UNSaved-value mapping was incorrect): [com.hibernate.student#ef3262e84974c56c014974c56dc60000]</span>
Timestamp (timestamp)
No need to specify optimistic-lock parameters in timestamp configuration
<?xml version= "1.0" encoding= "Utf-8"?> <!
DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
For timestamp test method and version principle method is basically the same, we can try it yourself, thank you!