{"record":{"id":"9615b79e3b05af44","repo":"hibernate/hibernate-orm","slug":"result-type-is-but-the-query-returned-a","errorCode":null,"errorMessage":"Result type is '{}' but the query returned a '{}'","messagePattern":"Result type is '(.+?)' but the query returned a '(.+?)'","errorType":"exception","errorClass":"QueryTypeMismatchException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerCheckingImpl.java","lineNumber":35,"sourceCode":" * @author Gavin King\n */\npublic class RowTransformerCheckingImpl<R> implements RowTransformer<R> {\n\n\tprivate final Class<R> type;\n\n\tpublic RowTransformerCheckingImpl(Class<R> type) {\n\t\tthis.type = type;\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tpublic R transformRow(Object[] row) {\n\t\tfinal Object result = row[0];\n\t\tif ( result == null || type.isInstance( result ) ) {\n\t\t\treturn (R) result;\n\t\t}\n\t\telse {\n\t\t\tthrow new QueryTypeMismatchException( \"Result type is '\" + type.getSimpleName()\n\t\t\t\t\t+ \"' but the query returned a '\" + result.getClass().getSimpleName() + \"'\" );\n\t\t}\n\t}\n\n\t@Override\n\tpublic int determineNumberOfResultElements(int rawElementCount) {\n\t\treturn 1;\n\t}\n}\n","sourceCodeStart":17,"sourceCodeEnd":45,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerCheckingImpl.java#L17-L45","documentation":"Thrown at row-transformation time by RowTransformerCheckingImpl: you gave `createQuery(hql, SomeClass.class)` a result type, and the first element of an actual result row is not an instance of that class. This transformer is Hibernate's fallback (ConcreteSqmSelectQueryPlan:372,376) whenever the requested type is not List/Map/Tuple/array and constructor-based transformation did not apply - so it is also the user-visible symptom of errors 3187/3188. Note only row[0] is checked and returned, which is why multi-column selects with a single class fail here.","triggerScenarios":"`em.createQuery(\"select e.name from Employee e\", Employee.class)` - the query returns String but Employee was requested; requesting a DTO class while selecting the entity (`select e from Employee e`, `EmployeeDto.class`); a DTO projection whose constructor does not match (constructor transformer fails, checking transformer takes over and the raw first value is checked); native query with mismatched resultClass.","commonSituations":"Wrong or leftover result-class parameter after refactoring a query from entity to scalar or vice versa; passing an interface the returned object does not implement; expecting Hibernate to auto-map multiple columns into a DTO without `select new` or a matching constructor.","solutions":["Pass the class that actually matches the first selected expression: `createQuery(\"select e.name from Employee e\", String.class)`","For multi-column DTO results use `select new com.acme.Dto(...)` or add a matching constructor so the constructor transformer applies","For arbitrary columns, query `Object[].class`, `List.class`, `Map.class`, or `Tuple.class` instead"],"exampleFix":"// before\nQuery q = em.createQuery(\"select e.name from Employee e\", Employee.class);\n// after\nQuery q = em.createQuery(\"select e.name from Employee e\", String.class);","handlingStrategy":"type-guard","validationCode":"// Know what the select actually returns: probe once with Object.class / Object[]\nObject probe = session.createQuery(\"select e.name from Employee e\", Object.class).setMaxResults(1).getSingleResult();\nif (!Employee.class.isInstance(probe) && wantedType == Employee.class) { /* adjust query or result class */ }","typeGuard":"// Narrow before use when mixing dynamic results\nstatic <R> R asResultType(Class<R> type, Object raw) {\n    return type.isInstance(raw) ? type.cast(raw) : null; // null = wrong result class, avoid QueryTypeMismatchException\n}","tryCatchPattern":"catch (QueryTypeMismatchException e) { // org.hibernate.query.QueryTypeMismatchException\n    // message names requested vs returned type; fix createQuery(hql, X.class) accordingly\n    throw e;\n}","preventionTips":["Make the result-class parameter of createQuery part of the test that runs the query","For single-value selects request the expression's type; for entity selects request the entity class","For multi-column results use Object[]/List/Map/Tuple - never a single bean class without a matching constructor"],"tags":["hql","jpql","result-type","type-mismatch","query","row-transformer"],"backgroundTag":"query-result-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}