hibernate/hibernate-orm · error · QueryTypeMismatchException
Result type is ' ' but the query returned a
Error message
Result type is '{}' but the query returned a '{}' What it means
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.
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
Example fix
// before
Query q = em.createQuery("select e.name from Employee e", Employee.class);
// after
Query q = em.createQuery("select e.name from Employee e", String.class); Defensive patterns
Strategy: type-guard
Validate before calling
// Know what the select actually returns: probe once with Object.class / Object[]
Object probe = session.createQuery("select e.name from Employee e", Object.class).setMaxResults(1).getSingleResult();
if (!Employee.class.isInstance(probe) && wantedType == Employee.class) { /* adjust query or result class */ } Type guard
// Narrow before use when mixing dynamic results
static <R> R asResultType(Class<R> type, Object raw) {
return type.isInstance(raw) ? type.cast(raw) : null; // null = wrong result class, avoid QueryTypeMismatchException
} Try / catch
catch (QueryTypeMismatchException e) { // org.hibernate.query.QueryTypeMismatchException
// message names requested vs returned type; fix createQuery(hql, X.class) accordingly
throw e;
} Prevention
- 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
When it happens
Trigger: `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.
Common situations: 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.
Related errors
- Cannot instantiate query result type, found no matching…
- Incorrect query result type: query produces
- Parameter of function ' ()' has type ' ', but argument is…
- unrecognized cast target type
- Cannot assign expression of type
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/9615b79e3b05af44.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerCheckingImpl.java:35
* @author Gavin King
*/
public class RowTransformerCheckingImpl<R> implements RowTransformer<R> {
private final Class<R> type;
public RowTransformerCheckingImpl(Class<R> type) {
this.type = type;
}
@Override
@SuppressWarnings("unchecked")
public R transformRow(Object[] row) {
final Object result = row[0];
if ( result == null || type.isInstance( result ) ) {
return (R) result;
}
else {
throw new QueryTypeMismatchException( "Result type is '" + type.getSimpleName()
+ "' but the query returned a '" + result.getClass().getSimpleName() + "'" );
}
}
@Override
public int determineNumberOfResultElements(int rawElementCount) {
return 1;
}
}
View on GitHub (pinned to fad1729dce)