hibernate/hibernate-orm · error · HibernateException

Attribute was neither a Collection nor a Map : ${erasedType}

Error message

Attribute was neither a Collection nor a Map : ${erasedType}

What it means

AutoApplicableConverterDescriptorStandardImpl resolves the element type of an attribute when matching collection auto-apply converters: it handles Map, Collection, and arrays, and throws HibernateException('Attribute was neither a Collection nor a Map : <erasedType>') for anything else. Reaching this branch means a non-standard container type arrived on a path that assumes a real collection.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AutoApplicableConverterDescriptorStandardImpl.java:85

					typeArguments( Map.class, collectionMemberType );
			if ( typeArguments.length < 2 ) {
				return null;
			}
			elementType = typeArguments[1];
		}
		else if ( Collection.class.isAssignableFrom( erasedType ) ) {
			final var typeArguments =
					typeArguments( Collection.class, collectionMemberType );
			if ( typeArguments.length == 0 ) {
				return null;
			}
			elementType = typeArguments[0];
		}
		else if ( erasedType.isArray() ) {
			elementType = erasedType.componentType();
		}
		else {
			throw new HibernateException( "Attribute was neither a Collection nor a Map : " + erasedType);
		}

		return isAssignableFrom( linkedConverterDescriptor.getDomainValueResolvedType(),
						canonicalizePrimitive( elementType ) )
				? linkedConverterDescriptor
				: null;
	}

	@Override
	public ConverterDescriptor<?,?> getAutoAppliedConverterDescriptorForMapKey(
			MemberDetails memberDetails,
			MetadataBuildingContext context) {

		final var collectionMemberType = actualMemberType( memberDetails );
		final Type keyType;
		if ( Map.class.isAssignableFrom( erasedType( collectionMemberType ) ) ) {
			final var typeArguments =
					typeArguments( Map.class, collectionMemberType );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Declare multi-valued fields as List, Set, Collection, Map, or an array instead of Iterable/custom containers
  2. If a custom container is required, make it implement java.util.Collection
  3. Avoid auto-apply converters for the element type of such fields; apply conversion explicitly or disable autoApply on the colliding converter
  4. If the field is a plain standard collection and you still hit this, capture a reproducer and report it to Hibernate (HHH) — it would be an internal resolution bug

Example fix

// before
@Entity
public class Order {
    private Iterable<OrderLine> lines; // Iterable is not a Collection
}

// after
@Entity
public class Order {
    private List<OrderLine> lines;
}
Defensive patterns

Strategy: type-guard

Type guard

static boolean isSupportedMultiValuedType(Class<?> c) {
    return java.util.Map.class.isAssignableFrom(c)
        || java.util.Collection.class.isAssignableFrom(c)
        || c.isArray();
}

Try / catch

catch (HibernateException e) {
    if (String.valueOf(e.getMessage()).startsWith("Attribute was neither a Collection nor a Map")) {
        throw new IllegalStateException("Change the field to List/Set/Collection/Map/array - " + e.getMessage(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: An attribute whose erased type implements neither java.util.Collection nor java.util.Map and is not an array — e.g. Iterable<T>, Stream<T>, or a custom container implementing only Iterable — while auto-apply converter scanning evaluates it as a collection site.

Common situations: Entity fields declared as Iterable for API convenience; custom/guava-style collection wrappers not extending java.util.Collection; code generated from XSDs/prototypes that model multi-valued fields as generic Iterables.

Related errors


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