A different object with the same Identifier value was already associated with the session error Solution

Source: Internet
Author: User

 

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 );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.