JPA NamedQuery 不会获取对已修改实体的更改
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了同样的问题,并发现了两件事:
和/或查询。
用于事务管理和查询的完全相同的对象
执行。 就我而言,我让 Mockito 监视实体管理器,
足以打破交易管理。
I've just had the same issue and discovered two things:
and / or the query.
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.
老实说,我对 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.