我正在使用 SpringBoot Jpa 与数据库表进行交互,在我的一些 JPA 存储库中,当其他应用程序从外部更新该特定数据库表时,我得到旧版本的 JPA 实体(未更新),JPA 无法给出更新的数据集。我尝试使用 EntityManager.clear()、entityManager.getEntityManagerFactory().getCache().evictAll();但他们中没有人在工作。
最后,我必须从entityManagerFactory 创建一个新的entityManager 对象并运行 native SQL 来获取更新的输出。但这对于我的项目来说不是一个好的实践,因为它使我的 JPA 依赖于 Native SQL。因此,如果我缺少任何内容或应该做些什么来反射(reflect)运行时 JPA 存储库的最新更改,请告诉我。 或者有什么方法可以从新创建的实体管理器对象运行 JPA 存储库?
entityManager.getEntityManagerFactory().getCache().evictAll(); //Autowired from persistenceContext
entityManager.clear();
EntityManager em=emf.createEntityManager();
最佳答案
尝试向服务添加@Transactional
注释。
此外,请确保您有一个实体实例。
关于java - 当从外部资源更新表时,Spring Data JPA 不会刷新或清除缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901245/