{"record":{"id":"9f85585d42ebe11b","repo":"hibernate/hibernate-orm","slug":"collection-loader-for-returned-an-instance-of","errorCode":null,"errorMessage":"Collection loader for '{}' returned an instance of '{}'","messagePattern":"Collection loader for '(.+?)' returned an instance of '(.+?)'","errorType":"exception","errorClass":"QueryTypeMismatchException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderNamedQuery.java","lineNumber":57,"sourceCode":"\t\tfinal var query = namedQueryMemento.toSelectionQuery( session );\n\t\t//noinspection unchecked\n\t\tquery.setParameter( (Parameter<Object>) query.getParameters().iterator().next(), key );\n\t\tquery.setQueryFlushMode( QueryFlushMode.NO_FLUSH );\n\t\tfinal List<?> resultList = query.getResultList();\n\t\t// TODO: we need a good way to inspect the query itself to see what it returns\n\t\tif ( !resultList.isEmpty() && resultList.get(0) instanceof PersistentCollection<?> persistentCollection ) {\n\t\t\t// in hbm.xml files we have the <load-collection/> element\n\t\t\treturn persistentCollection;\n\t\t}\n\t\telse {\n\t\t\t// using annotations, we have no way to specify a @CollectionResult\n\t\t\tfinal var collection =\n\t\t\t\t\tsession.getPersistenceContextInternal()\n\t\t\t\t\t\t\t.getCollection( new CollectionKey( persister, key ) );\n\t\t\tfor ( Object element : resultList ) {\n\t\t\t\tif ( element != null\n\t\t\t\t\t\t&& !persister.getElementType().getReturnedClass().isInstance( element ) ) {\n\t\t\t\t\tthrow new QueryTypeMismatchException( \"Collection loader for '\" + persister.getRole()\n\t\t\t\t\t\t\t+ \"' returned an instance of '\" + element.getClass().getName() + \"'\" );\n\t\t\t\t}\n\t\t\t}\n\t\t\tcollection.beforeInitialize( persister, resultList.size() );\n\t\t\tcollection.injectLoadedState( getLoadable(), resultList );\n\t\t\tcollection.afterInitialize();\n\t\t\tsession.getPersistenceContextInternal()\n\t\t\t\t\t.getCollectionEntry( collection )\n\t\t\t\t\t.postInitialize( collection, session );\n\t\t\treturn collection;\n\t\t}\n\t}\n}\n","sourceCodeStart":39,"sourceCodeEnd":71,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderNamedQuery.java#L39-L71","documentation":"Error \"Collection loader for '{}' returned an instance of '{}'\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at hibernate-core/src/main/java/org/hibernate/loader/ast/internal/CollectionLoaderNamedQuery.java:57 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the named query returns the expected collection type for the collection loader; correct the named query or the collection mapping."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}