hibernate/hibernate-orm · error · NonUniqueObjectException

A different object with the same identifier value was…

Error message

A different object with the same identifier value was already associated with this persistence context for entity {infoString}

What it means

Thrown by checkUniqueness when the persistence context already contains a different managed instance (not the same reference) for the EntityKey being associated. Fires during flush/load when two distinct Java objects carry the same identifier, typically from passing a detached copy while the original is still managed in the session.

Solutions

  1. Use session.merge() instead of persist() when another instance with the same id is already loaded
  2. Clear or evict the existing instance before associating a new object with the same identifier
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:806 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:806

							.$$_hibernate_getInterceptor();
			if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor ) {
				lazinessInterceptor.forceInitialize( maybeProxy, null );
			}
			return maybeProxy;
		}
		else {
			return maybeProxy;
		}
	}

	@Override
	public void checkUniqueness(EntityKey key, Object object) throws HibernateException {
		final Object entity = getEntity( key );
		if ( entity == object ) {
			throw new AssertionFailure( "object already associated, but no entry was found" );
		}
		if ( entity != null ) {
			throw new NonUniqueObjectException( key.getIdentifier(), key.getEntityName() );
		}
	}

	@Override
	public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object)
			throws HibernateException {

		final var concreteProxyClass = persister.getConcreteProxyClass();
		final boolean alreadyNarrow = concreteProxyClass.isInstance( proxy );

		if ( !alreadyNarrow ) {
			PERSISTENCE_CONTEXT_LOGGER.narrowingProxy( concreteProxyClass );

			// If an impl is passed, there is really no point in creating a proxy.
			// It would just be extra processing.  Just return the impl
			if ( object != null ) {
				removeProxyByKey( key );
				return object;

View on GitHub (pinned to fad1729dce)