我有一个名为user的实体,它是版本控制的:
@Entity
public class User{
...
@Version
private Long version;还有一个叫取款,那就不是了。
在@Singleton类中的周期性@Transactional方法中,我尝试根据用户执行的提款来更新用户:
for(Withdrawal withdrawal : withdrawals ) {
User user= entityManager.find(User.class, withdrawal.getUser().getId());
user.setFunds(user.getFunds() - withdrawal .getFunds());
entityManager.merge(user);
withdrawal.setStatus(Withdrawal.Status.OK);
entityManager.merge(withdrawal);
}如果我在用户被读取之后,但在他被合并之前更改了他的版本,我会得到一个异常:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction对用户的更改被恢复,但对撤销的更改没有恢复,这是一个问题。我做错了什么?
发布于 2015-03-27 20:29:31
可能有许多可能的原因。您的数据库是事务性的吗?您是否在数据库和应用程序中都启用了事务?您如何获取您的取款对象?它们是托管实体吗?
https://stackoverflow.com/questions/29300577
复制相似问题