hibernate/hibernate-orm · error · DuplicateMappingException

Duplicate SQL result set mapping '{}'

Error message

Duplicate SQL result set mapping '{}'

What it means

DuplicateMappingException(Type.RESULT_SET_MAPPING): applyResultSetMapping detected (via the map put returning a previous value) that the same result-set-mapping name is registered twice. @SqlResultSetMapping names must be unique per persistence unit because native queries reference them by name.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java:849

		}
		else {
			final String name = resultSetMappingDescriptor.getRegistrationName();
			if ( name == null ) {
				throw new IllegalArgumentException( "Result-set mapping name is null: " + resultSetMappingDescriptor );
			}
			else if ( !defaultSqlResultSetMappingNames.contains( name ) ) {
				applyResultSetMapping( resultSetMappingDescriptor );
			}
		}
	}

	public void applyResultSetMapping(NamedResultSetMappingDescriptor resultSetMappingDescriptor) {
		final var old = sqlResultSetMappingMap.put(
				resultSetMappingDescriptor.getRegistrationName(),
				resultSetMappingDescriptor
		);
		if ( old != null ) {
			throw new DuplicateMappingException(
					DuplicateMappingException.Type.RESULT_SET_MAPPING,
					resultSetMappingDescriptor.getRegistrationName()
			);
		}
	}

	@Override
	public void addDefaultResultSetMapping(NamedResultSetMappingDescriptor definition) {
		final String name = definition.getRegistrationName();
		if ( !defaultSqlResultSetMappingNames.contains( name ) ) {
			sqlResultSetMappingMap.remove( name );
		}
		applyResultSetMapping( definition );
		defaultSqlResultSetMappingNames.add( name );
	}


	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View on GitHub (pinned to fad1729dce)

Solutions

  1. Search the name from the message and rename one mapping, then update the resultSetMapping reference that points at it
  2. Remove duplicated mapping-file entries in persistence.xml
  3. Keep query and mapping names paired by convention (X.query / X.mapping) so copies get renamed together

Example fix

// before — mapping name copied unchanged to a second entity
@SqlResultSetMapping(name = "userResult", columns = ...)
@SqlResultSetMapping(name = "userResult", columns = ...)

// after
@SqlResultSetMapping(name = "userResult", columns = ...)
@SqlResultSetMapping(name = "orderResult", columns = ...) // and update resultSetMapping="orderResult"
Defensive patterns

Strategy: validation

Validate before calling

static void assertNoDuplicateResultSetMappingNames( Class<?>... entityClasses ) {
    final Map<String, Class<?>> seen = new HashMap<>();
    for ( final Class<?> c : entityClasses ) {
        for ( final SqlResultSetMapping m : c.getAnnotationsByType( SqlResultSetMapping.class ) ) {
            final Class<?> prev = seen.put( m.name(), c );
            if ( prev != null ) {
                throw new IllegalStateException( "Duplicate @SqlResultSetMapping '" + m.name() + "' on " + prev + " and " + c );
            }
        }
    }
}

Try / catch

try {
    metadata.buildSessionFactory();
} catch ( DuplicateMappingException e ) {
    if ( e.getType() == DuplicateMappingException.Type.RESULT_SET_MAPPING ) {
        throw new IllegalStateException( "Duplicate SQL result set mapping '" + e.getName() + "' — rename one mapping and its referencing query", e );
    }
    throw e;
}

Prevention

When it happens

Trigger: The same @SqlResultSetMapping(name="X") on two entities — classic when a native-query setup is copied between entities and only the query is renamed; an orm.xml file loaded twice; annotation plus XML duplicates of the same mapping.

Common situations: Copy-pasted native query + mapping pairs, shared library jars, and duplicated persistence.xml <mapping-file> entries.

Related errors


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