hibernate/hibernate-orm · error · HibernateException

merge requested with id not matching id of passed entity

Error message

merge requested with id not matching id of passed entity

What it means

getDetachedEntityId: an explicit id was supplied to merge(EntityClass, object, id) but it does not equal the id carried by the passed detached entity. The two identifiers must match for merge to proceed; the inputs are the requested id and the entity's own id.

Solutions

  1. Do not change the id of the entity being merged to a value different from the one requested.
  2. Pass the entity whose id matches the id used to look up the managed copy.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown when a merge is explicitly given an id that differs from the id of the passed entity instance.

Common situations: Typical situations: copy/paste of ids between entities; wrong entity passed together with an id from another instance.


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

Appendix: source

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

		else {
			return target;
		}
	}

	@Nullable
	private static Object getDetachedEntityId(
			@Nonnull MergeEvent event,
			@Nullable Object originalId,
			@Nonnull EntityPersister persister) {
		final var source = event.getSession();
		final Object id = event.getRequestedId();
		if ( id == null ) {
			return originalId;
		}
		else {
			// check that entity id = requestedId
			if ( !persister.getIdentifierType().isEqual( id, originalId, source.getFactory() ) ) {
				throw new HibernateException( "merge requested with id not matching id of passed entity" );
			}
			return id;
		}
	}

	@Nonnull
	private static Object unproxyManagedForDetachedMerging(
			@Nonnull Object incoming,
			@Nonnull Object managed,
			@Nonnull EntityPersister persister,
			@Nonnull EventSource source) {
		if ( isHibernateProxy( managed ) ) {
			return assertInitialized( managed );
		}

		if ( isPersistentAttributeInterceptable( incoming )
				&& persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {

View on GitHub (pinned to fad1729dce)