使用JPA merge()时的OptimisticLockException

Uro*_*s K 12 java hibernate jpa

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

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

    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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. update(String,T):进行更新的通用方法.检查id是否匹配并执行合并操作.

    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);
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

为什么会这样?实现这一目标的正确方法是什么?

我有点不想将属性复制newStoreexistingStoreexistingStore用于合并 - 我认为这将解决乐观锁定问题.

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

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

Sau*_*ext 1

我无法从您添加的代码中看到您的实体,但我相信您错过了乐观锁定的一些关键点 - >@Version版本字段上的注释。如果您的实体上有此字段,那么容器应该能够毫无问题地执行合并过程。请看看 乐观锁定也是一篇好文章,不要破坏乐观锁定