Skip to content

Commit 3e02a41

Browse files
committed
HHH-19547 fix an exception message that has been fixed and unfixed many times
"oid" never meant "old id"
1 parent 73dbec3 commit 3e02a41

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,26 @@ public void injectCallbackRegistry(CallbackRegistry callbackRegistry) {
7171
/**
7272
* make sure user didn't mangle the id
7373
*/
74-
public void checkId(Object object, EntityPersister persister, Object id, SessionImplementor session)
74+
public void checkId(Object object, EntityPersister persister, EntityEntry entry, SessionImplementor session)
7575
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+
}
10091
}
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
10194
}
10295

10396
private void checkNaturalId(
@@ -179,7 +172,7 @@ else if ( !mightBeDirty && loadedState != null ) {
179172
}
180173
else {
181174
final EntityPersister persister = entry.getPersister();
182-
checkId( entity, persister, entry.getId(), session );
175+
checkId( entity, persister, entry, session );
183176
// grab its current state
184177
Object[] values = persister.getValues( entity );
185178
checkNaturalId( persister, entity, entry, values, loadedState, session );

0 commit comments

Comments
 (0)