Hibernate学习:乐观锁version
一:程序
二:分析
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]
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。