hibernate/hibernate-orm · error · IllegalStateException

Map instantiation has arguments with duplicate aliases [{}]

Error message

Map instantiation has arguments with duplicate aliases [{}]

What it means

Thrown by DynamicInstantiationResultImpl.createResultAssembler when a `select new map(...)` query contains two or more arguments sharing the same alias. The message lists the offending aliases joined by commas. It fires at query-plan creation, before SQL execution, because duplicate keys cannot be represented in the resulting Map.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationResultImpl.java:140

			boolean areAllArgumentsAliased,
			boolean areAnyArgumentsAliased,
			List<String> duplicatedAliases,
			List<ArgumentReader<?>> argumentReaders,
			AssemblerCreationState creationState) {

		if ( nature == DynamicInstantiationNature.LIST ) {
			if ( LOG.isDebugEnabled() && areAnyArgumentsAliased ) {
				LOG.debug( "One or more arguments for List dynamic instantiation (`new list(...)`) specified an alias; ignoring" );
			}
			return (DomainResultAssembler<R>)
					new DynamicInstantiationAssemblerListImpl( (JavaType<List<?>>) javaType, argumentReaders );
		}
		else if ( nature == DynamicInstantiationNature.MAP ) {
			if ( ! areAllArgumentsAliased ) {
				throw new IllegalStateException( "Map instantiation contained one or more arguments with no alias" );
			}
			if ( !duplicatedAliases.isEmpty() ) {
				throw new IllegalStateException(
						"Map instantiation has arguments with duplicate aliases ["
								+ StringHelper.join( ",", duplicatedAliases ) + "]"
				);
			}
			return (DomainResultAssembler<R>)
					new DynamicInstantiationAssemblerMapImpl( (JavaType<Map<?,?>>) javaType, argumentReaders );
		}
		else {
			return assembler( areAllArgumentsAliased, duplicatedAliases, argumentReaders, creationState );
		}
	}

	private DomainResultAssembler<R> assembler(
			boolean areAllArgumentsAliased,
			List<String> duplicatedAliases,
			List<ArgumentReader<?>> argumentReaders,
			AssemblerCreationState creationState) {
		// find a constructor matching argument types

View on GitHub (pinned to fad1729dce)

Solutions

  1. Rename the duplicate aliases so every argument has a unique key, e.g. `as customerId` and `as orderId`
  2. Check the exception message - it names the exact duplicated aliases to fix
  3. For generated queries, derive aliases from expression plus position to avoid collisions

Example fix

// before
select new map(o.id as id, c.id as id) from Order o join o.customer c
// after
select new map(o.id as orderId, c.id as customerId) from Order o join o.customer c
Defensive patterns

Strategy: validation

Validate before calling

// Fail fast on duplicate aliases before first execution
Set<String> unique = new HashSet<>();
boolean dup = !unique.addAll(aliasesOfNewMapArguments(hql));
if (dup) throw new IllegalArgumentException("Duplicate aliases: " + aliasesOfNewMapArguments(hql).stream().filter(a -> Collections.frequency(aliasesOfNewMapArguments(hql), a) > 1).toList());

Prevention

When it happens

Trigger: `select new map(e.id as id, e.name as id) from Employee e`; joining several tables and using the same alias name (e.g. `as name`) for columns of different tables; alias placeholders like `as col` repeated in generated HQL.

Common situations: Copy-paste errors in hand-written HQL; alias collisions only noticed when the branch of code with that query first executes; schema-wide naming where many tables have identically named columns and aliases are reused.

Related errors


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