{"record":{"id":"dc188fa6720fc3dc","repo":"hibernate/hibernate-orm","slug":"cannot-instantiate-query-result-type-dc188f","errorCode":null,"errorMessage":"Cannot instantiate query result type","messagePattern":"Cannot instantiate query result type","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerConstructorImpl.java","lineNumber":68,"sourceCode":"\n\tprivate static Class<?> resolveElementJavaType(TupleElement<?> element) {\n\t\tif ( element instanceof SqmExpressibleAccessor<?> accessor ) {\n\t\t\tfinal SqmExpressible<?> expressible = accessor.getExpressible();\n\t\t\tif ( expressible != null && expressible.getExpressibleJavaType() != null ) {\n\t\t\t\treturn expressible.getExpressibleJavaType().getJavaTypeClass();\n\t\t\t}\n\t\t}\n\n\t\treturn element.getJavaType();\n\t}\n\n\t@Override\n\tpublic T transformRow(Object[] row) {\n\t\ttry {\n\t\t\treturn constructor.newInstance( row );\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new InstantiationException( \"Cannot instantiate query result type\", type, e );\n\t\t}\n\t}\n\n\t@Override\n\tpublic int determineNumberOfResultElements(int rawElementCount) {\n\t\treturn 1;\n\t}\n}\n","sourceCodeStart":50,"sourceCodeEnd":77,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerConstructorImpl.java#L50-L77","documentation":"Thrown from RowTransformerConstructorImpl.transformRow: a matching constructor was found, but invoking it via reflection on a result row threw. The original exception (e.g. NullPointerException from inside the DTO constructor, IllegalArgumentException from a null passed to a primitive parameter, IllegalAccessException under JPMS restrictions) is attached as the cause - always read `getCause()`. Unlike 3187/3188 this happens per-row at execution time, so the query plan built fine and some or all rows fail.","triggerScenarios":"DTO constructor that throws on unexpected values (nulls, empty strings, invalid enum text parsed inside the constructor); a null database value passed to a primitive constructor parameter; constructor made non-public or module not opened so reflective access fails; data-driven failures that only occur for specific rows (bad enum name, null column).","commonSituations":"Defensive DTO constructors with Objects.requireNonNull on nullable columns; upgrading to JDK 16+ where reflective access to non-public classes is denied unless the package is opened; new rows with nulls in columns that were previously NOT NULL.","solutions":["Inspect the wrapped cause (`e.getCause()`) - it names the real failure inside the constructor","Make constructor parameters nullable-safe: use wrapper types instead of primitives, handle nulls explicitly","Open the DTO package for reflection (`--add-opens java.base/...` or `opens` in module-info) or make the class and constructor public","Fix or filter the offending data (e.g. `where e.closedOn is not null`)"],"exampleFix":"// before\npublic ReportDto(String title, int pages) { this.title = Objects.requireNonNull(title); }\n// after\npublic ReportDto(String title, Integer pages) { this.title = title; this.pages = pages == null ? 0 : pages; }","handlingStrategy":"try-catch","validationCode":"// Before mapping rows, null-check nullable columns your constructor rejects\nif (row[0] == null) { /* substitute default or skip row */ }","typeGuard":null,"tryCatchPattern":"try { dto = query.getSingleResult(); }\ncatch (org.hibernate.InstantiationException e) {\n    Throwable real = e.getCause(); // the exception thrown inside the constructor\n    // handle NPE/IAE from bad data, or reflective access failures\n}","preventionTips":["DTO constructors must tolerate nulls: wrapper types, explicit null handling, no Objects.requireNonNull on nullable columns","Make DTO classes and constructors public (or open the package under JPMS) so setAccessible works","Log the failing row's raw values when wrapping this exception so data issues are diagnosable"],"tags":["dto","constructor","reflection","null-handling","row-transformer","jpms"],"backgroundTag":"dto-constructor-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}