java – OptimisticLockException当使用JPA merge()

前端之家收集整理的这篇文章主要介绍了java – OptimisticLockException当使用JPA merge()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个休息的应用程序,其中一个资源可以更新.以下是负责实现此任务的两种方法

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

@H_403_4@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匹配并执行合并操作.

@H_403_4@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
原文链接:https://www.f2er.com/java/125401.html

猜你在找的Java相关文章