3

多くのグーグル検索の後、休止状態のバージョンをダウングレードすることを除いて、質問に対する答えが見つかりませんでした。しかし、2003年の同様の投稿でこの状況に直面しました。

何の問題:

//in the  first session I do
session1.save(entity);
session1.getTransaction().commit();
session1.close();

//in the second session (after client response), I get back the serialized copy of the entity
entityCopy = deserialize(jsonString);
entityCopy.setEntityDetail(newDetail); //1
session2.merge(entityCopy); //EXCEPTION!

コメント文字列 1 の場合、すべて正常に動作します。

例外:

IllegalStateException :エンティティ #4700 の保存中にエラーが発生しました エンティティ コピー #4700 は、別のエンティティ @2e7b134a に既に割り当てられています

質問:

  1. 私の状況で何が問題になっていますか?
  2. 私が理解しているように、エンティティのコピーが既にキャッシュにある場合、merge() 操作はそのような場合に実装されました。私が間違っている?

PS

  1. 重要な場合はEntity -> EntityDetaillazy、orphanRemoval = true、1 対 1 の関係でリンクされています
  2. equals() および hashCode() メソッドをオーバーライドしました。
4

1 に答える 1

1

この問題を次の方法で解決します。変更を加える前に、デシリアライズされたエンティティをマージする必要があります。(唯一の変更は 2 文字列にあります):

//in the  first session I do
session1.save(entity);
session1.getTransaction().commit();
session1.close();

//in the second session (after client response), I get back the serialized copy of the entity
entityCopy = deserialize(jsonString);
entityCopy = (Entity) session.merge(entityCopy); //2
entityCopy.setEntityDetail(newDetail); 
session2.merge(entityCopy); //all works fine
于 2013-08-13T06:14:16.630 に答える