我有一个使用NamedQuery
. 我更新每个实体的值,然后运行另一个命名查询(以相同的方法和Transaction
)过滤旧值,它返回相同的实体,就好像我没有更改它们一样。
我知道EntityManager
需要刷新并且它应该自动发生,但这没有任何区别。
我启用了休眠 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