使用版本的Hibernate乐观锁定不起作用



我正试图使用版本字段使用乐观锁定,当我从jpa存储库调用save时,不会引发异常。我是春天和冬眠的新手,我担心我的设置不正确
我正在使用的库有:hibernate4 maven插件1.0.2版本hibernate-jpa02.0 1.0.1春季数据jpa 1.3.4版

所以我的实体是这样设置的:

@Entity
public class MyEntity
{
@Id
protected Long id;
@Version
protected Long version;
protected String name;
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(Long id)
{
this.name = name;
}
}

我通过dto将版本传递给客户端,并在MyEntityStoreDao:中进行保存时将其传递回来

@Repository
public class MyEntityStoreDao extends BaseDao<MyEntityStoreDao>
{
private RepositoryManager myRepoManager;
@Autowired
public void setMyRepo(MyEntityRepository myRepo)
{
this.myRepo = myRepo;
}
public MyEntity save(MyEntityDTO dtoToUpdate)
{
Session session = this.Session();
MyEntity myEntity = new MyEntity();
if(dtoToUpdate.getId() > 0) {
myEntity = (MyEntity) session.get(MyEntity.class, dtoToUpdate.getId())  
}      
myEntity.setName(dtoToUpdate.getName());
MyEntity result = this.myRepo.save(myEntity); 
this.repositoryManager.flush(myRepo);
}
}

repositoryManager位于BaseDao中,并且正在使用org.springframework.data.jpa.repository.JpaRepository

版本正在正确更新并递增。但当我进行更新时,我预计当从DTO传递到MyEntityStoreDao中保存的版本与数据库中的版本不匹配时,它会抛出StaleStateException或OptmisticLockingException。

我检查了,版本不匹配,但保存仍然存在。为什么会发生这种情况,有什么帮助吗?感谢

通过显示sql=true打开sql日志记录,并查看更新查询是否具有所需的where子句

where version = ?

如果缺少这样的where子句,则需要添加注释@org.hibernate.annotations.Entity(dynamicUpdate = true)

使用更新的代码

MyEntity myEntity = new MyEntity(); // You dont need to initalize
if(dtoToUpdate.getId() > 0) {
myEntity = (MyEntity) session.get(MyEntity.class, dtoToUpdate.getId())  
}      
myEntity.setName(dtoToUpdate.getName());
MyEntity result = this.myRepo.save(myEntity); 

您正在尝试保存myEntity,该实体具有数据库中的最新信息(正确版本)。所以你不会得到任何错误。如果你想产生错误,请执行以下操作。。

public MyEntity save(MyEntityDTO dtoToUpdate)
{
Session session = this.Session();
MyEntity myEntityV1 = null;
MyEntity myEntityV2 = null;
// Getting v1 and V2. at this time both V1 & V2 will have same version ( assume version as 5)
if(dtoToUpdate.getId() > 0) {
myEntityV1 = (MyEntity) session.get(MyEntity.class, dtoToUpdate.getId())  ;
myEntityV2 = (MyEntity) session.get(MyEntity.class, dtoToUpdate.getId())  ;
}      
myEntityV1.setName(dtoToUpdate.getName());
// Saving V1 will reflect the increase in version  ( actual row will be version of 6)
MyEntity result = this.myRepo.save(myEntityV1);   
myEntityV2.setName("some changes"); // change some in V2instance. So that hibernate/Jpa will capture the change
this.myRepo.save(myEntityV2);    // You will get exception. because the v2 has version as 5. but the row was updated .
this.repositoryManager.flush(myRepo);
}

所以基本上,当你更新Db中的实体时,如果实体版本(对象中的变量)不等于数据库中的版本(表中的字段)(在更新之前),它会抛出异常

最新更新