码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate学习:乐观锁version

时间:2015-02-09 14:12:27      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:乐观锁   hibernate   

一:程序


二:分析


1:在实体中增加字段version

@Version
private int version;


2:Account a1 = (Account) session1.load(Account.class, 1);

Account a2 = (Account) session2.load(Account.class, 1);

version默认为0

 当session1.getTransaction().commit();version字段增加1,从0变成1

当session2.getTransaction().commit(); 原来获取的version是0,但是现在数据库已经完成1,不匹配,就会报错


org.hibernate.StaleObjectStateException:
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
 [com.hibernate.model.Account#1]



 
 
 

Hibernate学习:乐观锁version

标签:乐观锁   hibernate   

原文地址:http://blog.csdn.net/u013628152/article/details/43669469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!