Add a version field to the table and POJO to indicate the recorded version, so that multiple users can change a data conflict at the same time.
Database script:
Create table studentVersion (id varchar (32), name varchar (32), ver int );
POJO
Package Version;
Public class Student {
Private String id;
Private String name;
Private int version;
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getVersion (){
Return version;
}
Public void setVersion (int version ){
This. version = version;
}
}
Student. hbm. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping file autogenerated by MyEclipse-Hibernate Tools
-->
<Hibernate-mapping>
<Class name = "Version. Student" table = "studentVersion">
<Id name = "id" unsaved-value = "null">
<Generator class = "uuid. hex"> </generator>
</Id>
<! -- The version label must be behind the id label -->
<Version name = "version" column = "ver" type = "int"> </version>
<Property name = "name" type = "string" column = "name"> </property>
</Class>
</Hibernate-mapping>
Hibernate. cfg. xml
<? Xml version = '1. 0' encoding = 'utf-8'?>
<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<! -- Generated by MyEclipse Hibernate Tools. -->
<Hibernate-configuration>
<Session-factory>
<Property name = "connection. username"> root </property>
<Property name = "connection. url">
Jdbc: mysql: // localhost: 3306/schoolproject? CharacterEncoding = gb2312 & amp; useUnicode = true
</Property>
<Property name = "dialect">
Org. hibernate. dialect. MySQLDialect
</Property>
<Property name = "myeclipse. connection. profile"> mysql </property>
<Property name = "connection. password"> 1234 </property>
<Property name = "connection. driver_class">
Com. mysql. jdbc. Driver
</Property>
<Property name = "hibernate. dialect">
Org. hibernate. dialect. MySQLDialect
</Property>
<Property name = "hibernate. show_ SQL"> true </property>
<Property name = "current_session_context_class"> thread </property>
<Property name = "jdbc. batch_size"> 15 </property>
<Mapping resource = "Version/Student. hbm. xml"/>
</Session-factory>
</Hibernate-configuration>
Test code:
Package Version;
Import java. io. File;
Import java. util. Iterator;
Import java. util. Set;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. Transaction;
Import org. hibernate. cfg. Configuration;
Public class Test {
Public static void main (String [] args ){
String filePath = System. getProperty ("user. dir") + File. separator + "src/Version" + File. separator + "hibernate. cfg. xml ";
File file = new File (filePath );
System. out. println (filePath );
SessionFactory sessionFactory = new Configuration (). configure (file). buildSessionFactory ();
Session session = sessionFactory. openSession ();
Transaction t = session. beginTransaction ();
Student stu = new Student ();
Stu. setName ("tom11 ");
Session. save (stu );
T. commit ();
/*
* Simulate Multiple session operations on the student data table
*/
Session session1 = sessionFactory. openSession ();
Session session2 = sessionFactory. openSession ();
Student stu1 = (Student) session1.createQuery ("from Student s where s. name = 'tom11'"). uniqueResult ();
Student stu2 = (Student) session2.createQuery ("from Student s where s. name = 'tom11'"). uniqueResult ();
// At this time, the two versions are the same
System. out. println ("v1 =" + stu1.getVersion () + "-- v2 =" + stu2.getVersion ());
Transaction tx1 = session1.beginTransaction ();
Stu1.setName ("session1 ");
Tx1.commit ();
// At this time, the two versions are different, and one of them increases progressively
System. out. println ("v1 =" + stu1.getVersion () + "-- v2 =" + stu2.getVersion ());
Transaction tx2 = session2.beginTransaction ();
Stu2.setName ("session2 ");
Tx2.commit ();
}
}
Running result:
Hibernate: insert into studentVersion (ver, name, id) values (?, ?, ?)
Hibernate: select student0 _. id as id0 _, student0 _. ver as ver0 _, student0 _. name as name0 _ from studentVersion student0 _ where student0 _. name = 'tom11'
Hibernate: select student0 _. id as id0 _, student0 _. ver as ver0 _, student0 _. name as name0 _ from studentVersion student0 _ where student0 _. name = 'tom11'
V1 = 0 -- v2 = 0
Hibernate: update studentVersion set ver = ?, Name =? Where id =? And ver =?
V1 = 1 -- v2 = 0
Hibernate: update studentVersion set ver = ?, Name =? Where id =? And ver =?
Exception in thread "main" org. hibernate. StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Version. Student # comment]
As you can see, when the second "user" session2 modifies data, the recorded version number has been updated by session1, so a red exception is thrown, we can handle this exception in practical applications. For example, we can re-read the data in the database during processing, and display the current data with the data in the database so that users can have a chance to compare it, or the design program automatically reads new data.
Note: If you manually set stu. setVersion () to update the version by yourself to skip the check, this optimistic lock will become invalid. You can set the setVersion of Student. java to private