【问题标题】:SpringData JPA is not refreshing or clear the cache, when table is updated from outside resource当从外部资源更新表时,Spring Data JPA 不会刷新或清除缓存
【发布时间】: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


    【解决方案1】:

    尝试在服务中添加@Transactional注解。

    另外,请确保您只有一个实体实例。

    【讨论】:

    • 请分享更多细节,以便我们提供帮助。
    【解决方案2】:

    我认为您应该添加以下代码:

    entityManager.flush();

    【讨论】:

    • 试过了,还是不行
    猜你喜欢
    • 2019-04-04
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2019-01-14
    • 2018-04-20
    相关资源
    最近更新 更多