大多数基于数据版本记录机制(version)实现,一般是在数据库表中加入一个version字段
读取数据时将版本号一同读出,之后更新数据时版本号加一,如果提交数据时版本号小于或等于数据表中
的版本号,则认为数据是过期的,否则给予更新
Inventory.hbm.xml
1<?xml version="1.0"?>
2<!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping>
6 <!--注意:要加上optimistic-lock="version"-->
7 <class name="com.bjsxt.hibernate.Inventory" table="t_inventory" optimistic-lock="version">
8 <id name="itemNo">
9 <generator class="native"/>
10 </id>
11 <version name="version"/>
12 <property name="itemName"/>
13 <property name="quantity"/>
14 </class>
15</hibernate-mapping>
1<?xml version="1.0"?>
2<!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping>
6 <!--注意:要加上optimistic-lock="version"-->
7 <class name="com.bjsxt.hibernate.Inventory" table="t_inventory" optimistic-lock="version">
8 <id name="itemNo">
9 <generator class="native"/>
10 </id>
11 <version name="version"/>
12 <property name="itemName"/>
13 <property name="quantity"/>
14 </class>
15</hibernate-mapping>
OptimisticLockingTest.java
1package com.bjsxt.hibernate;
2
3import org.hibernate.LockMode;
4import org.hibernate.Session;
5
6import junit.framework.TestCase;
7
8public class OptimisticLockingTest extends TestCase {
9
10 public void testLoad1() {
11 Session session = null;
12 try {
13 session = HibernateUtils.getSession();
14 session.beginTransaction();
15
16 Inventory inv = (Inventory)session.load(Inventory.class, 1);
17 System.out.println("itemName=" + inv.getItemName());
18 System.out.println("version=" + inv.getVersion());
19 System.out.println("quantity=" + inv.getQuantity());
20 inv.setQuantity(inv.getQuantity() - 200);
21 session.update(inv);
22 session.getTransaction().commit();
23 }catch(Exception e) {
24 e.printStackTrace();
25 session.getTransaction().rollback();
26 }finally {
27 HibernateUtils.closeSession(session);
28 }
29 }
30
31 public void testLoad2() {
32 Session session = null;
33 try {
34 session = HibernateUtils.getSession();
35 session.beginTransaction();
36
37 Inventory inv = (Inventory)session.load(Inventory.class, 1);
38 System.out.println("itemName=" + inv.getItemName());
39 System.out.println("version=" + inv.getVersion());
40 System.out.println("quantity=" + inv.getQuantity());
41 inv.setQuantity(inv.getQuantity() - 200);
42 session.update(inv);
43 session.getTransaction().commit();
44 }catch(Exception e) {
45 e.printStackTrace();
46 session.getTransaction().rollback();
47 }finally {
48 HibernateUtils.closeSession(session);
49 }
50 }
51
52}
53//把testLoad1进行debug,然后运行testLoad2可以得到效果!
54
1package com.bjsxt.hibernate;
2
3import org.hibernate.LockMode;
4import org.hibernate.Session;
5
6import junit.framework.TestCase;
7
8public class OptimisticLockingTest extends TestCase {
9
10 public void testLoad1() {
11 Session session = null;
12 try {
13 session = HibernateUtils.getSession();
14 session.beginTransaction();
15
16 Inventory inv = (Inventory)session.load(Inventory.class, 1);
17 System.out.println("itemName=" + inv.getItemName());
18 System.out.println("version=" + inv.getVersion());
19 System.out.println("quantity=" + inv.getQuantity());
20 inv.setQuantity(inv.getQuantity() - 200);
21 session.update(inv);
22 session.getTransaction().commit();
23 }catch(Exception e) {
24 e.printStackTrace();
25 session.getTransaction().rollback();
26 }finally {
27 HibernateUtils.closeSession(session);
28 }
29 }
30
31 public void testLoad2() {
32 Session session = null;
33 try {
34 session = HibernateUtils.getSession();
35 session.beginTransaction();
36
37 Inventory inv = (Inventory)session.load(Inventory.class, 1);
38 System.out.println("itemName=" + inv.getItemName());
39 System.out.println("version=" + inv.getVersion());
40 System.out.println("quantity=" + inv.getQuantity());
41 inv.setQuantity(inv.getQuantity() - 200);
42 session.update(inv);
43 session.getTransaction().commit();
44 }catch(Exception e) {
45 e.printStackTrace();
46 session.getTransaction().rollback();
47 }finally {
48 HibernateUtils.closeSession(session);
49 }
50 }
51
52}
53//把testLoad1进行debug,然后运行testLoad2可以得到效果!
54