hibernate/hibernate-orm · error · InstantiationException
Cannot instantiate query result type
Error message
Cannot instantiate query result type
What it means
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.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerConstructorImpl.java:68
private static Class<?> resolveElementJavaType(TupleElement<?> element) {
if ( element instanceof SqmExpressibleAccessor<?> accessor ) {
final SqmExpressible<?> expressible = accessor.getExpressible();
if ( expressible != null && expressible.getExpressibleJavaType() != null ) {
return expressible.getExpressibleJavaType().getJavaTypeClass();
}
}
return element.getJavaType();
}
@Override
public T transformRow(Object[] row) {
try {
return constructor.newInstance( row );
}
catch (Exception e) {
throw new InstantiationException( "Cannot instantiate query result type", type, e );
}
}
@Override
public int determineNumberOfResultElements(int rawElementCount) {
return 1;
}
}
View on GitHub (pinned to fad1729dce)
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`)
Example fix
// before
public ReportDto(String title, int pages) { this.title = Objects.requireNonNull(title); }
// after
public ReportDto(String title, Integer pages) { this.title = title; this.pages = pages == null ? 0 : pages; } Defensive patterns
Strategy: try-catch
Validate before calling
// Before mapping rows, null-check nullable columns your constructor rejects
if (row[0] == null) { /* substitute default or skip row */ } Try / catch
try { dto = query.getSingleResult(); }
catch (org.hibernate.InstantiationException e) {
Throwable real = e.getCause(); // the exception thrown inside the constructor
// handle NPE/IAE from bad data, or reflective access failures
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Unable to generate set of all hints - {}
- Could not instantiate entity
- Could not instantiate managed bean directly
- Error performing the dynamic instantiation
- Error instantiating class '{}' using default constructor: {}
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/dc188fa6720fc3dc.
Report an issue: GitHub.