hibernate/hibernate-orm · error · HibernateException
An immutable natural identifier of entity
Error message
An immutable natural identifier of entity %s was altered from `%s` to `%s`
What it means
Immutability check at flush for single-attribute natural ids: the natural id is declared immutable, but its current value differs from the loaded snapshot — it was altered despite @NaturalId(mutable=false).
Solutions
- Do not change an immutable natural id; update other attributes only, or delete and re-create the entity with the new natural id.
- Declare the natural id mutable if changes must be allowed.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at flush time when an entity's immutable simple natural id attribute has been changed compared to its loaded snapshot.
Common situations: See trigger scenarios.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/09e970c0c1ff9beb.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SimpleNaturalIdMapping.java:76
public SingularAttributeMapping getAttribute() {
return attribute;
}
@Override
public void verifyFlushState(
Object id,
Object[] currentState,
Object[] loadedState,
SharedSessionContractImplementor session) {
if ( !isMutable() ) {
final var persister = getDeclaringType().getEntityPersister();
final Object naturalId = extractNaturalIdFromEntityState( currentState );
final Object snapshot =
loadedState == null
? session.getPersistenceContextInternal().getNaturalIdSnapshot( id, persister )
: persister.getNaturalIdMapping().extractNaturalIdFromEntityState( loadedState );
if ( !areEqual( naturalId, snapshot, session ) ) {
throw new HibernateException(
String.format(
"An immutable natural identifier of entity %s was altered from `%s` to `%s`",
persister.getEntityName(),
snapshot,
naturalId
)
);
}
}
// otherwise, the natural id is mutable (!immutable), no need to do the checks
}
@Override
public Object extractNaturalIdFromEntityState(Object[] state) {
if ( state == null ) {
return null;
}
else if ( state.length == 1 ) {View on GitHub (pinned to fad1729dce)