JPA删除后未从数据库获取更新的数据

时间:2020-01-29 07:47:19

标签: java database jpa find entitymanager

我有一个Entity,可以称之为EntityParent。该EntityParent具有一个EntityChildrens列表。通过查询接收到我的EntityParent并将其存储到全局变量后,我想使用EntityManager.remove()方法从列表中删除EntityChildren。到目前为止,这个工作正常。当我想使用EntityManager.find(id)方法获取更新的EntityParent时,我正在使用列表中所有EntityChilds的旧EntityParent,即使它已在数据库中删除。

我尝试使用findAll()方法将所有EntityParents存储在列表中。如果我在使用findAll()方法之后使用方法find(id),则获取更新后的EntityParent而不删除EntityChild。是什么原因呢?以及如何在不从数据库中获取整个列表的情况下获取数据?

如果不清楚的地方问一下。如果需要,我还可以提供一些示例代码。

编辑: 我也正在得到:

Caused by: Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.OptimisticLockException Exception Description: The object cannot be updated because it has changed or been deleted since it was last read.

当我想编辑我的实体时。可能是因为我正在访问从EntityManager获取的View中的变量吗?

0 个答案:

没有答案