hibernate/hibernate-orm · error · HibernateException

Immutable attribute ' ' of entity ' ' was modified (mark…

Error message

Immutable attribute '%s' of entity '%s' was modified (mark the field non-'final' or remove the '@Immutable' annotation)

What it means

verifyImmutableAttributes compared source and target values on merge and found a property marked @Immutable (or final in records) whose value differs between the detached copy and the managed target. Immutable attributes cannot be updated; the offending input is the modified immutable field value.

Solutions

  1. Do not modify attributes of an @Immutable entity; treat it as read-only.
  2. Remove the @Immutable annotation (or make the field non-final) if the attribute must be mutable.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown when a change is detected on an attribute or entity mapped as immutable.

Common situations: Typical situations: modifying a field annotated @Immutable; dirty-checking detecting a mutation on an immutable attribute during merge/flush.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/bf70bf00e5733390. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java:508

					() -> interceptor.postMerge( entity, target, id, targetValues, originalValues, propertyNames, propertyTypes ) );
			//copyValues works by reflection, so explicitly mark the entity instance dirty
			markInterceptorDirty( entity, target );
			event.setResult( result );
		}
	}

	private void verifyImmutableAttributes(
			@Nonnull EntityPersister persister,
			@Nonnull Object[] sourceValues,
			@Nonnull Object[] targetValues,
			@Nonnull EventSource session) {
		for ( int i : persister.getImmutablePropertyIndexes() ) {
			final Object sourceValue = sourceValues[i];
			final Object targetValue = targetValues[i];
			if ( sourceValue != UNFETCHED_PROPERTY && targetValue != UNFETCHED_PROPERTY ) {
				if ( !persister.getPropertyTypes()[i]
						.isEqual( sourceValue, targetValue, session.getFactory() ) ) {
					throw new HibernateException( "Immutable attribute '%s' of entity '%s' was modified"
							.formatted( persister.getPropertyNames()[i], persister.getEntityName() )
									+ " (mark the field non-'final' or remove the '@Immutable' annotation)");
				}
			}
		}
	}

	@Nonnull
	private static Object targetEntity(
			@Nonnull MergeEvent event,
			@Nonnull Object entity,
			@Nonnull EntityPersister persister,
			@Nullable Object id,
			@Nonnull Object result) {
		final var source = event.getSession();
		final String entityName = persister.getEntityName();
		final Object target = unproxyManagedForDetachedMerging( entity, result, persister, source );
		if ( target == entity) {

View on GitHub (pinned to fad1729dce)