虽然saveOrUpdate失败,但版本增加了

时间:2012-03-29 20:44:37

标签: java spring hibernate version

我在SQL中创建了一个数据库表:

CREATE TABLE `s_supplier` ( <br/>
  `id` int(11) NOT NULL auto_increment,<br/>
  `code` varchar(32) collate utf8_bin NOT NULL,<br/>
  `name` varchar(128) collate utf8_bin NOT NULL,<br/>
  `version` int(11) NOT NULL default '0'<br/>
  PRIMARY KEY  (`id`),<br/>
  UNIQUE KEY `ui_uom_category_code` (`code`)<br/>
)

数据库现有2行:
(1,code1,name1,1)
(2,code2,name2,1)

使用spring-hibernate:
第1步:
我将第1行加载到实体,然后将代码更改为code2(与第2行相同)。 然后调用
org.springframework.orm.hibernate3.support.HibernateDaoSupport.getHibernateTemplate()。saveOrUpdate(entity);
- &GT;它引发了异常“重复代码” - &gt;好的

第2步: 我将代码更改为code3,然后通过上述方法再次保存 - &gt;它引发了异常'行更新或其他用户删除的乐观异常'(我只测试单个用户) - &gt; KO

跟踪后我发现在第1步之后版本会自动增加实体1。我认为不应该因为发生异常
我的问题:

  

这是spring framework还是hibernate的bug?

1 个答案:

答案 0 :(得分:1)

听起来不像是一个bug。我不知道它是否在Hibernate文档中有明确记录,但行为与JPA规范一致:

Transaction rollback typically causes the persistence context to be in an 
inconsistent state at the point of rollback. In particular, the state of 
version attributes and generated state (e.g., generated primary keys) may
be inconsistent.