Error: a different object with the same Identifier value was already associated with the session
I encountered this error again today. I know what is going on, but I forgot where I wrote it and corrected it.CodeTo avoid finding another one, write it here.
This error occurs because two identical identifiers but different entities exist in the same session in hibernate. that is to say, there are two entities with the same IDs in the memory, causing a conflict. (I query the object again before modifying it. In this case, there is an entity in the memory, when the modification is saved, an object with the same ID is submitted, so there are two entities with the same ID. The system does not know which object to call and reports an error.
Solution:
Clear the existing entities in the memory. The Code is as follows:
Public voidupdatebymerge (User user) {session = gethibernatetemplate (). getsessionfactory (). getcurrentsession (); Session. Clear (); Session. Update (User );}