hibernate/hibernate-orm · error · HibernateException

identifier of an instance of {entityName} was altered from {

Error message

identifier of an instance of {entityName} was altered from {oldValue} to {newValue}

What it means

Error "identifier of an instance of {entityName} was altered from {oldValue} to {newValue}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementAsProxyLazinessInterceptor.java:210

		if ( meta.identifierAttributeNames.contains( attributeName ) ) {
			// it is illegal for the identifier value to be changed.  Normally Hibernate
			// validates this during flush.  However, here it's dangerous to just allow the
			// new value to be set and continue on waiting for the flush for validation
			// because this interceptor manages the entity's entry in the PC itself.  So
			// just do the check here up-front
			final boolean changed;
			if ( meta.nonAggregatedCidMapper == null ) {
				changed = ! entityKey.getPersister().getIdentifierType().isEqual( oldValue, newValue );
			}
			else {
				final int subAttrIndex = meta.nonAggregatedCidMapper.getPropertyIndex( attributeName );
				final var subAttrType = meta.nonAggregatedCidMapper.getSubtypes()[subAttrIndex];
				changed = ! subAttrType.isEqual( oldValue, newValue );
			}

			if ( changed ) {
				throw new HibernateException( "identifier of an instance of " + entityKey.getEntityName()
						+ " was altered from " + oldValue + " to " + newValue );
			}

			// otherwise, setId has been called but passing in the same value - just pass it through
			return newValue;
		}

		if ( meta.initializeBeforeWrite
				|| meta.collectionAttributeNames != null && meta.collectionAttributeNames.contains( attributeName ) ) {
			// we need to force-initialize the proxy - the fetch group to which the `attributeName` belongs
			try {
				forceInitialize( target, attributeName );
			}
			finally {
				setInitialized();
			}

			if ( meta.inLineDirtyChecking ) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not change the identifier of a managed entity; create a new entity with the new id instead, or use a mutable business key separate from @Id.

Example fix

Do not change the identifier of a managed entity; create a new entity with the new id instead, or use a mutable business key separate from @Id.

When it happens

Trigger: Bytecode enhancement, proxy creation, or lazy interception is applied to an incompatible class or configuration.

Common situations: Build-time enhancement with module-system restrictions, mismatched Hibernate versions between build and runtime, or hibernate.bytecode.provider=none with a model that needs proxies.


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