@@ -67,9 +67,9 @@ public void injectCallbackRegistry(CallbackRegistry callbackRegistry) {
6767 }
6868
6969 /**
70- * make sure user didn't mangle the id
70+ * Make sure user didn't mangle the id.
7171 */
72- public void checkId (Object object , EntityPersister persister , Object id , SessionImplementor session )
72+ public void checkId (Object object , EntityPersister persister , Object id , Status status , SessionImplementor session )
7373 throws HibernateException {
7474
7575 if ( id instanceof DelayedPostInsertIdentifier ) {
@@ -79,22 +79,17 @@ public void checkId(Object object, EntityPersister persister, Object id, Session
7979 }
8080
8181 final Object oid = persister .getIdentifier ( object , session );
82-
8382 if ( id == null ) {
8483 throw new AssertionFailure ( "null id in " + persister .getEntityName ()
8584 + " entry (don't flush the Session after an exception occurs)" );
8685 }
87-
88- //Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
89- //(However it's not safe to invoke the equals() method as it might trigger side-effects)
90- if ( id == oid ) {
91- //No further checks necessary:
92- return ;
93- }
94-
95- if ( !persister .getIdentifierType ().isEqual ( id , oid , session .getFactory () ) ) {
86+ // Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
87+ // (However it's not safe to invoke the equals() method as it might trigger side effects.)
88+ else if ( id != oid
89+ && !status .isDeletedOrGone ()
90+ && !persister .getIdentifierType ().isEqual ( id , oid , session .getFactory () ) ) {
9691 throw new HibernateException ( "identifier of an instance of " + persister .getEntityName ()
97- + " was altered from " + oid + " to " + id );
92+ + " was altered from " + oid + " to " + id );
9893 }
9994 }
10095
@@ -177,9 +172,9 @@ else if ( !mightBeDirty && loadedState != null ) {
177172 }
178173 else {
179174 final EntityPersister persister = entry .getPersister ();
180- checkId ( entity , persister , entry .getId (), session );
175+ checkId ( entity , persister , entry .getId (), entry . getStatus (), session );
181176 // grab its current state
182- Object [] values = persister .getValues ( entity );
177+ final Object [] values = persister .getValues ( entity );
183178 checkNaturalId ( persister , entity , entry , values , loadedState , session );
184179 return values ;
185180 }
0 commit comments