---
title: "[转载]hibernate的乐观锁"
author: "dmwdmc"
date: "2013-06-18T08:35:20Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2013-06-17T22:44:41Z"
id: 1888425
url: "https://dmwdmc.iteye.com/blog/1888425"
---

[http://esffor.iteye.com/blog/168243](http://esffor.iteye.com/blog/168243)

 

通过在表中及POJO中增加一个version字段来表示记录的版本，来达到多用户同时更改一条数据的冲突

数据库脚本：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)createtable studentVersion (id varchar(32),name varchar(32),ver int);

POJO

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package Version;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclass Student ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)private String id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)private String name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateint version;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getId() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoid setId(String id) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.id = id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getName() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoid setName(String name) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.name = name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicint getVersion() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return version;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoid setVersion(int version) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.version = version;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

Student.hbm.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="utf-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!--  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) Mapping file autogenerated by MyEclipse - Hibernate Tools  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;hibernate-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;class name="Version.Student" table="studentVersion"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;id name="id" unsaved-value="null"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;generator class="uuid.hex"&gt;&lt;/generator&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/id&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!--version标签必须跟在id标签后面--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;version name="version" column="ver" type="int"&gt;&lt;/version&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="name" type="string" column="name"&gt;&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/hibernate-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

Hibernate.cfg.xml

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE hibernate-configuration PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!-- Generated by MyEclipse Hibernate Tools. --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;hibernate-configuration&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;session-factory&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="connection.username"&gt;root&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="connection.url"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&amp;useUnicode=true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="dialect"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) org.hibernate.dialect.MySQLDialect  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="myeclipse.connection.profile"&gt;mysql&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="connection.password"&gt;1234&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="connection.driver\_class"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) com.mysql.jdbc.Driver  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="hibernate.dialect"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) org.hibernate.dialect.MySQLDialect  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="hibernate.show\_sql"&gt;true&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="current\_session\_context\_class"&gt;thread&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;property name="jdbc.batch\_size"&gt;15&lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;mapping resource="Version/Student.hbm.xml"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/session-factory&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/hibernate-configuration&gt;

 

测试代码：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package Version;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Iterator;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Set;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.Session;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.SessionFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.Transaction;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.cfg.Configuration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclass Test ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoid main(String\[] args) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) String filePath=System.getProperty("user.dir")+File.separator+"src/Version"+File.separator+"hibernate.cfg.xml";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) File file=new File(filePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.out.println(filePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Session session=sessionFactory.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Transaction t=session.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Student stu=new Student();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) stu.setName("tom11");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) session.save(stu);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) t.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 模拟多个session操作student数据表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Session session1=sessionFactory.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Session session2=sessionFactory.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Student stu1=(Student)session1.createQuery("from Student s where s.name='tom11'").uniqueResult();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Student stu2=(Student)session2.createQuery("from Student s where s.name='tom11'").uniqueResult();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//这时候，两个版本号是相同的  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.out.println("v1="+stu1.getVersion()+"--v2="+stu2.getVersion());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Transaction tx1=session1.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) stu1.setName("session1");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) tx1.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//这时候，两个版本号是不同的，其中一个的版本号递增了  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.out.println("v1="+stu1.getVersion()+"--v2="+stu2.getVersion());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Transaction tx2=session2.beginTransaction();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) stu2.setName("session2");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) tx2.commit();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

运行结果：

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#4028818316cd6b460116cd6b50830001]

 

可以看到，第二个“用户”session2修改数据时候，记录的版本号已经被session1更新过了，所以抛出了红色的异常，我们可以在实际应用中处理这个异常，例如在处理中重新读取数据库中的数据，同时将目前的数据与数据库中的数据展示出来，让使用者有机会比较一下，或者设计程序自动读取新的数据

 

注意：如果手工设置stu.setVersion()自行更新版本以跳过检查,则这种乐观锁就会失效，应对方法可以将Student.java的setVersion设置成private