@@ -70,28 +70,26 @@ public void injectCallbackRegistry(CallbackRegistry callbackRegistry) {
7070 /**
7171 * Make sure user didn't mangle the id.
7272 */
73- public void checkId (Object object , EntityPersister persister , Object id , Status status , SessionImplementor session )
73+ public void checkId (Object object , EntityPersister persister , EntityEntry entry , SessionImplementor session )
7474 throws HibernateException {
75-
76- if ( id instanceof DelayedPostInsertIdentifier ) {
77- // this is a situation where the entity id is assigned by a post-insert generator
78- // and was saved outside the transaction forcing it to be delayed
79- return ;
80- }
81-
82- final Object oid = persister .getIdentifier ( object , session );
83- if ( id == null ) {
84- throw new AssertionFailure ( "null id in " + persister .getEntityName ()
85- + " entry (don't flush the Session after an exception occurs)" );
86- }
87- // Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
88- // (However it's not safe to invoke the equals() method as it might trigger side effects.)
89- else if ( id != oid
90- && !status .isDeletedOrGone ()
91- && !persister .getIdentifierType ().isEqual ( id , oid , session .getFactory () ) ) {
92- throw new HibernateException ( "identifier of an instance of " + persister .getEntityName ()
93- + " was altered from " + oid + " to " + id );
75+ final Object entryId = entry .getId ();
76+ if ( entryId == null ) {
77+ throw new AssertionFailure ( "Entry for instance of '" + persister .getEntityName ()
78+ + "' has a null identifier (this can happen if the session is flushed after an exception occurs)" );
79+ }
80+ if ( !(entryId instanceof DelayedPostInsertIdentifier ) ) {
81+ final Object currentId = persister .getIdentifier ( object , session );
82+ // Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
83+ // (However it's not safe to invoke the equals() method as it might trigger side effects.)
84+ if ( entryId != currentId
85+ && !entry .getStatus ().isDeletedOrGone ()
86+ && !persister .getIdentifierType ().isEqual ( entryId , currentId , session .getFactory () ) ) {
87+ throw new HibernateException ( "Identifier of an instance of '" + persister .getEntityName ()
88+ + "' was altered from " + entryId + " to " + currentId );
89+ }
9490 }
91+ // else this is a situation where the entity id is assigned by a post-insert
92+ // generator and was saved outside the transaction, forcing it to be delayed
9593 }
9694
9795 private void checkNaturalId (
@@ -174,7 +172,7 @@ private Object[] getValues(Object entity, EntityEntry entry, boolean mightBeDirt
174172 }
175173 else {
176174 final EntityPersister persister = entry .getPersister ();
177- checkId ( entity , persister , entry . getId (), entry . getStatus () , session );
175+ checkId ( entity , persister , entry , session );
178176 // grab its current state
179177 final Object [] values = persister .getValues ( entity );
180178 checkNaturalId ( persister , entity , entry , values , loadedState , session );
0 commit comments