java – OptimisticLockException当使用JPA merge()

我有一个休息的应用程序,其中一个资源可以更新.以下是负责实现此任务的两种方法

> updateWithRelatedEntities(String,Store):接收通过反序列化PUT请求实体构造的id和新对象Store,在新对象上设置版本(用于乐观锁定),并在事务中调用更新.

public Store updateWithRelatedEntities(String id,Store newStore) {
    Store existingStore = this.get(id);

    newStore.setVersion(existingStore.getVersion());

    em.getTransaction().begin();
    newStore = super.update(id,newStore);
    em.getTransaction().commit();

    return newStore;
}

> update(String,T):进行更新的通用方法.检查ids匹配并执行合并操作.

public T update(String id,T newObj) {
   if (newObj == null) {
    throw new EmptyPayloadException(type.getSimpleName());
   }


   Type superclass = getClass().getGenericSuperclass();

   if (superclass instanceof Class) {
       superclass = ((Class) superclass).getGenericSuperclass();
   }

   Class<T> type = (Class<T>) (((ParameterizedType) superclass).getActualTypeArguments()[0]);

   T obj = em.find(type,id);

   if (!newObj.getId().equals(obj.getId())) {
       throw new IdMismatchException(id,newObj.getId());
   }

   return em.merge(newObj);
}

问题是这个调用:T obj = em.find(type,id);触发数据库中存储对象的更新,这意味着我们在触发合并时获得OptimisticLockException(因为版本现在不同).

为什么会发生这种情况?实现这一点的正确方法是什么?

我不想将属性从newStore复制到existingStore,并使用existingStore进行合并 – 我认为这将解决乐观锁定问题.

代码未在应用程序服务器上运行,并且我没有使用JTA.

编辑:
如果我在调用update之前分离existingStore,T obj = em.find(type,id);不会触发store对象的更新,这样可以解决问题.问题仍然存在 – 为什么当实体不分离时它会触发它?

解决方法

我看不到你的实体从您添加代码,但我相信你失去了一些关键点与乐观锁定> @Version注释版本字段.
如果您的实体上有这个字段,那么容器应该能够执行合并过程而没有问题.请看看
Optimistic Locking也很好的文章 don’t break optimistic locking

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...