hibernate/hibernate-orm · error · QueryTypeMismatchException

Collection loader for '{}' returned an instance of '{}'

Error message

Collection loader for '{}' returned an instance of '{}'

What it means

Error "Collection loader for '{}' returned an instance of '{}'" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderNamedQuery.java:57

		final var query = namedQueryMemento.toSelectionQuery( session );
		//noinspection unchecked
		query.setParameter( (Parameter<Object>) query.getParameters().iterator().next(), key );
		query.setQueryFlushMode( QueryFlushMode.NO_FLUSH );
		final List<?> resultList = query.getResultList();
		// TODO: we need a good way to inspect the query itself to see what it returns
		if ( !resultList.isEmpty() && resultList.get(0) instanceof PersistentCollection<?> persistentCollection ) {
			// in hbm.xml files we have the <load-collection/> element
			return persistentCollection;
		}
		else {
			// using annotations, we have no way to specify a @CollectionResult
			final var collection =
					session.getPersistenceContextInternal()
							.getCollection( new CollectionKey( persister, key ) );
			for ( Object element : resultList ) {
				if ( element != null
						&& !persister.getElementType().getReturnedClass().isInstance( element ) ) {
					throw new QueryTypeMismatchException( "Collection loader for '" + persister.getRole()
							+ "' returned an instance of '" + element.getClass().getName() + "'" );
				}
			}
			collection.beforeInitialize( persister, resultList.size() );
			collection.injectLoadedState( getLoadable(), resultList );
			collection.afterInitialize();
			session.getPersistenceContextInternal()
					.getCollectionEntry( collection )
					.postInitialize( collection, session );
			return collection;
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Ensure the named query returns the expected collection type for the collection loader; correct the named query or the collection mapping.

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderNamedQuery.java:57 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/9f85585d42ebe11b. Report an issue: GitHub.