hibernate/hibernate-orm · error · HibernateException
Identifier of an instance of
Error message
Identifier of an instance of '${persister.getEntityName()}' was altered from ${entryId} to ${currentId} What it means
During flush, the entity's current identifier no longer equals the identifier recorded in its EntityEntry (and the entity is not deleted), meaning application code mutated the id of a managed entity. Hibernate forbids altering identifiers of managed instances; the offending input is the changed id value.
Solutions
- Do not modify the identifier (@Id) property of a managed entity after it has been persisted; identifiers must be immutable.
- If a new identifier is required, delete the old row and insert a new entity with the new id.
- Check for code (setters, reflection, or deserialization) that assigns a new id to an already-persistent instance.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown when dirty checking detects that a managed entity's identifier value was changed.
Common situations: Typical situations: assigning a new value to the @Id property of a persistent entity; reflection or deserialization altering the id field.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/ed4ee9179a448eb8.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java:88
final Object currentId = persister.getIdentifier( object, session );
// Small optimization: always try to avoid getIdentifierType().isEqual(..) when possible.
// (However, it's not safe to invoke the equals() method as it might trigger side effects.)
if ( entryId != currentId
&& !entry.getStatus().isDeletedOrGone()
&& !persister.getIdentifierType().isEqual( entryId, currentId, session.getFactory() ) ) {
throwIdentifiedAlteredException( persister, entryId, currentId );
}
}
// else this is a situation where the entity id is assigned by a post-insert
// generator and was saved outside the transaction, forcing it to be delayed
}
// Used by Hibernate Reactive
protected void throwIdentifiedAlteredException(
@Nonnull EntityPersister persister,
@Nonnull Object entryId,
@Nonnull Object currentId) {
throw new HibernateException( "Identifier of an instance of '" + persister.getEntityName()
+ "' was altered from " + entryId + " to " + currentId );
}
private void checkNaturalId(
@Nonnull EntityPersister persister,
@Nonnull Object entity,
@Nonnull EntityEntry entry,
@Nonnull Object[] current,
@Nullable Object[] loaded,
@Nonnull SessionImplementor session) {
if ( !isUninitializedEnhanced( entity ) ) {
final var naturalIdMapping = persister.getNaturalIdMapping();
if ( naturalIdMapping != null && entry.getStatus() != Status.READ_ONLY ) {
naturalIdMapping.verifyFlushState( entry.getId(), current, loaded, session );
}
}
}
View on GitHub (pinned to fad1729dce)