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.