Org.hibernate.nonuniqueobjectexception:org.hibernate.nonuniqueobjectexception:a different obj exception resolution method

Source: Internet
Author: User

Abnormal:

Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with the Session

: A different object with the same identifier value already associated with the session

Code snippet:
Note: The following is the status of Session open
* I have manually constructed a photo object
Photo Photo=new photo ();
Photo.setid (1);
* Then I load the photo object with the same ID with load
Photo p=session.load (photo.class,1);
Now we have a P object with ID 1 in the first level cache.
* Then I do delete photo object
Session.delete (photo);
When I go straight this delete throws an exception, for what?
Because when I do delete photo object, it will go to delete the first level to find there is no photo ID of the photo object, this photo is I manually created, it's ID 1, so good, It goes to the first-level cache and just finds an object of type 1, but it refers to P, not photo, so the exception is thrown, and the exception means: There are two identities, but not the same reference object. So it cannot perform the delete.
* Change Code
Session.delete (P);
Test passed.
If you also encounter the same anomaly, overwhelmed, you can use Session.clean to clear the cache. Oh.

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.