JPA NamedQuery 不会获取对已修改实体的更改

发布于 2024-07-22 01:22:08 字数 941 浏览 6 评论 0原文

我有一个使用 NamedQuery 检索实体的方法。 我更新每个实体的值,然后运行另一个命名查询(使用相同的方法和事务)按旧值进行过滤,它返回相同的实体,就好像我没有更改它们一样。

我知道 EntityManager 需要刷新,而且它应该自动发生,但这没有任何区别。

我启用了 hibernate SQL 日志记录,并且可以看到,当我调用刷新时,但在容器事务提交时,实体没有更新。

EntityManager entityManager = getPrimaryEntityManager();
MyEntity myEntity = entityManager.find(MyEntityImpl.class, allocationId);
myEntity.setStateId(State.ACTIVE);
// Flush the entity manager to pick up any changes to entity states before we run this query.
entityManager.flush();
Query countQuery = entityManager
        .createNamedQuery("MyEntity.getCountByState");
// we're telling the persistence provider that we want the query to do automatic flushing before this
// particular query is executed.
countQuery.setParameter("stateId", State.CHECKING);
Long count = (Long) countQuery.getSingleResult();
// Count should be zero but isn't. It doesn't see my change above

I have a method that retrieves Entities using a NamedQuery. I update a value of each entity and then run another named query (in the same method and Transaction) filtering by the old value and it returns the same Entities as if I had not changed them.

I understand that the EntityManager needs to be flushed and also that it should happen automatically but that doesn't make any difference.

I enabled hibernate SQL logging and can see that the Entities are not updated when I call flush but when the container transaction commits.

EntityManager entityManager = getPrimaryEntityManager();
MyEntity myEntity = entityManager.find(MyEntityImpl.class, allocationId);
myEntity.setStateId(State.ACTIVE);
// Flush the entity manager to pick up any changes to entity states before we run this query.
entityManager.flush();
Query countQuery = entityManager
        .createNamedQuery("MyEntity.getCountByState");
// we're telling the persistence provider that we want the query to do automatic flushing before this
// particular query is executed.
countQuery.setParameter("stateId", State.CHECKING);
Long count = (Long) countQuery.getSingleResult();
// Count should be zero but isn't. It doesn't see my change above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我鬧 2024-07-29 01:22:09

我刚刚遇到了同样的问题,并发现了两件事:

  • 首先,您应该检查持久性上下文的 FlushMode
    和/或查询。
  • 其次,确保实体经理是
    用于事务管理和查询的完全相同的对象
    执行。 就我而言,我让 Mockito 监视实体管理器,
    足以打破交易管理。

I've just had the same issue and discovered two things:

  • Firstly, you should check the FlushMode for the persistence context
    and / or the query.
  • Secondly, make sure that the entity manager is
    exactly the same object for both transaction management and query
    execution. In my case, I had Mockito spy on the entityManager, which
    was enough to break the transaction management.
用心笑 2024-07-29 01:22:08

老实说,我对 JPA 不太熟悉,但我在使用 Hiberate 的会话管理器时遇到了类似的问题。 我的修复方法是在再次查询之前手动从 Hibernate 会话中删除指定的对象,这样它就被迫从数据库中进行查找,并且不会从缓存中获取该对象。 您可以尝试使用 JPA 的 EntityManager 执行相同的操作。

To be honest I'm not that familiar with JPA, but I ran into similar problems with Hiberate's session manager. My fix was to manually remove the specified object from Hibernate's session before querying on it again so it's forced to do a lookup from the database and doesn't get the object from cache. You might try doing the same with JPA's EntityManager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文