{"record":{"id":"e854cd5763c8f9b9","repo":"hibernate/hibernate-orm","slug":"output-type-s-cannot-be-assigned-to-requested-t","errorCode":null,"errorMessage":"Output type [%s] cannot be assigned to requested type [%s]","messagePattern":"Output type \\[(.+?)\\] cannot be assigned to requested type \\[(.+?)\\]","errorType":"exception","errorClass":"TypeMismatchException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/procedure/internal/ResultSetOutputImpl.java","lineNumber":59,"sourceCode":"\t\treturn true;\n\t}\n\n\t@Override\n\tpublic <X> ResultSetOutput<X> asResultSetOutput(Class<X> resultType) {\n\t\tif ( resultSetMapping == null\n\t\t\t\t|| (resultSetMapping.isDynamic() && resultSetMapping.getNumberOfResultBuilders() == 0) ) {\n\t\t\tresultSetMapping = Util.makeResultSetMapping(\n\t\t\t\t\tnull,\n\t\t\t\t\tresultType,\n\t\t\t\t\tnull,\n\t\t\t\t\t() -> sessionFactory\n\t\t\t);\n\t\t}\n\t\telse if ( resultSetMapping.getNumberOfResultBuilders() == 1 ) {\n\t\t\tvar resultBuilder = resultSetMapping.getResultBuilders().get( 0 );\n\t\t\tvar outputJavaType = resultBuilder.getJavaType();\n\t\t\tif ( outputJavaType != null && !resultType.isAssignableFrom( outputJavaType ) ) {\n\t\t\t\tthrow new TypeMismatchException( String.format( Locale.ROOT,\n\t\t\t\t\t\t\"Output type [%s] cannot be assigned to requested type [%s]\",\n\t\t\t\t\t\toutputJavaType.getName(),\n\t\t\t\t\t\tresultType.getName()\n\t\t\t\t) );\n\t\t\t}\n\t\t}\n\n\t\t//noinspection unchecked\n\t\treturn (ResultSetOutput<X>) this;\n\t}\n\n\t@Override\n\tpublic <X> ResultSetOutput<X> asResultSetOutput(jakarta.persistence.sql.ResultSetMapping<X> japMMapping) {\n\t\tthis.resultSetMapping = JpaMappingHelper.toHibernateMapping( japMMapping, sessionFactory );\n\t\t//noinspection unchecked\n\t\treturn (ResultSetOutput<X>) this;\n\t}\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/procedure/internal/ResultSetOutputImpl.java#L41-L77","documentation":"ResultSetOutputImpl checks the single result builder of the resolved result set mapping against the Class you request when reading outputs: if the mapping's outputJavaType is not assignable to the requested resultType, it throws TypeMismatchException. Requesting a supertype of the mapped type is fine; requesting an unrelated or sibling type is not.","triggerScenarios":"outputs.getCurrent().as(MyDto.class) / getOutputList(MyDto.class) when the mapping has exactly one result builder whose Java type is a different class (e.g., an entity from @EntityResult, a scalar from @ColumnResult) and resultType.isAssignableFrom(outputJavaType) is false.","commonSituations":"A @SqlResultSetMapping built around @EntityResult(Order.class) but the caller reads outputs as a DTO; mapping yields a scalar (BigDecimal) while the caller requests the entity class; DTO refactoring moved the mapped target class; switching from dynamic mapping to an explicit mapping without updating the requested type.","solutions":["Request the exact type the mapping produces, or one of its supertypes","Align the result class passed to createStoredProcedureCall(name, resultClass) with what the @SqlResultSetMapping actually returns","For DTO projections, define a @SqlResultSetMapping with @ConstructorResult(targetClass = MyDto.class) and request MyDto.class","If the shape is uncertain, request Object[].class or Tuple instead of a concrete class"],"exampleFix":"// before\nList<OrderReport> rows = outputs.getCurrent().as(OrderReport.class).getResultList(); // mapping yields Order entity\n\n// after\nList<Order> rows = outputs.getCurrent().as(Order.class).getResultList();\n// or add: @SqlResultSetMapping(name=\"orderReport\",\n//   classes = @ConstructorResult(targetClass = OrderReport.class, columns = {...}))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static <X> boolean resultTypeSafe(Class<?> mappedType, Class<X> requested) {\n    return requested.isAssignableFrom(mappedType);\n}","tryCatchPattern":"try {\n    List<Order> rows = outputs.getCurrent().as(Order.class).getResultList();\n} catch (org.hibernate.TypeMismatchException e) {\n    // requested type is incompatible with the mapping: request the mapped type or fix the @SqlResultSetMapping\n}","preventionTips":["Request exactly the type the result set mapping produces, or a supertype","Keep @SqlResultSetMapping definitions and the requested result classes in one reviewed place","Use @ConstructorResult for DTO projections so the mapped type equals the requested type"],"tags":["hibernate","stored-procedure","result-mapping","type-mismatch"],"backgroundTag":"result-set-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}