【发布时间】:2020-01-14 00:12:37
【问题描述】:
我正在使用 SpringBoot Jpa 与数据库表进行交互,在我的几个 JPA 存储库中,当其他应用程序从外部更新该特定数据库表时,我正在获取旧版本的 JPA 实体(未更新),JPA 无法给出更新的数据集。我尝试使用 EntityManager.clear(), entityManager.getEntityManagerFactory().getCache().evictAll();但不是他们工作。
最后,我必须从 entityManagerFactory 创建一个新的 entityManager 对象并运行本机 SQL 以获取更新后的输出。但这对我的项目来说不是一个好的做法,因为它使我的 JPA 依赖于 Native SQL。因此,请让我知道是否有任何我遗漏或应该做的事情来反映我的 JPA 存储库在运行时的最新更改。 或者有什么方法可以让我从新创建的 entitymanager 对象运行 JPA 存储库?
entityManager.getEntityManagerFactory().getCache().evictAll(); //Autowired from persistenceContext
entityManager.clear();
EntityManager em=emf.createEntityManager();
【问题讨论】:
标签: java spring-boot jpa repository entitymanager