を使用してエンティティを取得するメソッドがありますNamedQuery
。各エンティティの値を更新してTransaction
から、古い値でフィルタリングする別の名前付きクエリを(同じメソッドで)実行すると、変更していない場合と同じエンティティが返されます。
フラッシュする必要があり、自動的に実行する必要があることも理解していEntityManager
ますが、違いはありません。
Hibernate SQLロギングを有効にしましたが、flushを呼び出したときではなく、コンテナートランザクションがコミットしたときに、エンティティが更新されていないことがわかります。
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