3

After a lot of googling I didn't find answer to my question, except downgrade hibernate version. But I faced this situation at similar post dated as 2003 year.

What the problem:

//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!

If comment string 1, all works fine!

Exception:

IllegalStateException: Error occurred while storing entity #4700 An entity copy #4700 was already assigned to a different entity @2e7b134a

Questions:

  1. What is wrong in my sitation?
  2. As I understand, merge() operation was implemented for those cases, when we alreadty have the entity copy in cache. Am I wrong?

PS

  1. If it is important Entity -> EntityDetail are linked with lazy, orphanRemoval = true, one-2-one relationship
  2. I overrided equals() and hashCode() methods.
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
VB_
  • 45,112
  • 42
  • 145
  • 293

1 Answers1

1

I solve this problem in the next way: it is necessary to merge deserialized entity before you proceed some changes to it. (the only change is in the 2 string):

//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
VB_
  • 45,112
  • 42
  • 145
  • 293