@@ -71,33 +71,26 @@ public void injectCallbackRegistry(CallbackRegistry callbackRegistry) {
71
71
/**
72
72
* make sure user didn't mangle the id
73
73
*/
74
- public void checkId (Object object , EntityPersister persister , Object id , SessionImplementor session )
74
+ public void checkId (Object object , EntityPersister persister , EntityEntry entry , SessionImplementor session )
75
75
throws HibernateException {
76
-
77
- if ( id instanceof DelayedPostInsertIdentifier ) {
78
- // this is a situation where the entity id is assigned by a post-insert generator
79
- // and was saved outside the transaction forcing it to be delayed
80
- return ;
81
- }
82
-
83
- final Object oid = persister .getIdentifier ( object , session );
84
-
85
- if ( id == null ) {
86
- throw new AssertionFailure ( "null id in " + persister .getEntityName ()
87
- + " entry (don't flush the Session after an exception occurs)" );
88
- }
89
-
90
- //Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
91
- //(However it's not safe to invoke the equals() method as it might trigger side-effects)
92
- if ( id == oid ) {
93
- //No further checks necessary:
94
- return ;
95
- }
96
-
97
- if ( !persister .getIdentifierType ().isEqual ( id , oid , session .getFactory () ) ) {
98
- throw new HibernateException ( "identifier of an instance of " + persister .getEntityName ()
99
- + " was altered from " + oid + " to " + id );
76
+ final Object entryId = entry .getId ();
77
+ if ( entryId == null ) {
78
+ throw new AssertionFailure ( "Entry for instance of '" + persister .getEntityName ()
79
+ + "' has a null identifier (this can happen if the session is flushed after an exception occurs)" );
80
+ }
81
+ if ( !(entryId instanceof DelayedPostInsertIdentifier ) ) {
82
+ final Object currentId = persister .getIdentifier ( object , session );
83
+ // Small optimisation: always try to avoid getIdentifierType().isEqual(..) when possible.
84
+ // (However it's not safe to invoke the equals() method as it might trigger side effects.)
85
+ if ( entryId != currentId
86
+ && !entry .getStatus ().isDeletedOrGone ()
87
+ && !persister .getIdentifierType ().isEqual ( entryId , currentId , session .getFactory () ) ) {
88
+ throw new HibernateException ( "Identifier of an instance of '" + persister .getEntityName ()
89
+ + "' was altered from " + entryId + " to " + currentId );
90
+ }
100
91
}
92
+ // else this is a situation where the entity id is assigned by a post-insert
93
+ // generator and was saved outside the transaction, forcing it to be delayed
101
94
}
102
95
103
96
private void checkNaturalId (
@@ -179,7 +172,7 @@ else if ( !mightBeDirty && loadedState != null ) {
179
172
}
180
173
else {
181
174
final EntityPersister persister = entry .getPersister ();
182
- checkId ( entity , persister , entry . getId () , session );
175
+ checkId ( entity , persister , entry , session );
183
176
// grab its current state
184
177
Object [] values = persister .getValues ( entity );
185
178
checkNaturalId ( persister , entity , entry , values , loadedState , session );
0 commit comments