Hibernate reports a different object with the same Identifier value was already associated with the session

Source: Internet
Author: User

I have encountered this exception before. I didn't know how to do it before, so I went online to check it. This time I encountered this exception again. I am determined to trace the source and find the origin of the problem.

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

This error means that two different objects are associated with the same flag, that is, two objects exist in a session, however, the flag spaces of these two objects are the same. For example, the IDs of the two objects are the same, and this is the unique primary key of the database. conflicts occur during the update operation, because hibernate does not know which object to update, this exception is reported.

 

The following answers are provided for searching on the Internet:

Solution 1: Session. Clean ()
PS: If saveorupdate (object) and other operations to change the data status are performed after the clean operation, the "found two representations of same Collection" exception may be reported.
Solution 2: Session. Refresh (object)
PS: when the object is not an existing data object in the database, the session cannot be used. refresh (object) because this method is used to retrieve the object from the hibernate session, if this object does not exist in the session, an error is returned. Therefore, when you use saveorupdate (object) you need to judge before.
Solution 3: Session. Merge (object)
PS: the built-in methods in hibernate are recommended. My analysis: We usually write this exception in the program. First, we get the object passed from the page. We call it VO (view object ), then we call the hibernate method to query the database and obtain the entity class that is the same as the VO primary key. We call it Po, and then we get the attribute in the Po and assign the value to the VO attribute, finally, we call the hibernate method to update Vo, and this exception is reported. The reason for this is that the cache of the hibernate session contains both the VO object and the Po object, and their IDs are the same, so hibernate does not know the update. The final solution is to operate on the object class Po queried from the database, and call the Set Method in the Po object class to copy the property to be updated, in this way, an object will be maintained in the hibernate session. Do not create an object completely, or assign VO to the object class. This error will also be reported. Because after copying or new objects, there will be two more objects in the hibernate session.

 

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.