1

I am using JPA EntityManager with Spring/Hibernate and entitymanager seems to be behaving weird when I update an entity from a hibernate session and then view it from a different hibernate session.

I have the following configuration in my application:

  1. EHCache, and enabled Second Level Cache
  2. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) on the entities
  3. Extended PersistenceContext on em
  4. OpenEntityManagerInViewFilter

Calling em.clear() solves this issue. I tried disabling second level caching, query caching etc but did not help. I expected the entitymanager to take care of this. Is this expected ? I am looking for a cleaner approach, and do away with calling em.clear().

4

2 に答える 2

2

これらの古いエンティティは、トランザクションとは関係なく、拡張永続コンテキストによって管理されます。JPA 2.0仕様では、これは次のように綴られています。

新しいトランザクションが開始されると、拡張永続コンテキスト内の管理対象オブジェクトはデータベースから再ロードされないことに注意してください。

それらを自動的に更新する組み込み機能はありません。実行できるのは、単一のエンティティに対してclearまたはEntityManager.refreshを使用することです。また、マッピングでREFRESH操作をカスケードすることもできます。

于 2012-07-19T10:36:07.533 に答える
0

問題の根本原因を見つけました。これは、私の更新とフェッチが 2 つの異なるサーブレット コンテキストから行われていたためです。私は今、それらを同じコンテキストに入れましたが、うまくいきました。

于 2012-07-19T17:04:59.297 に答える