hibernate/hibernate-orm · error · TypeMismatchException

Output type [%s] cannot be assigned to requested type [%s]

Error message

Output type [%s] cannot be assigned to requested type [%s]

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/procedure/internal/ResultSetOutputImpl.java:59

		return true;
	}

	@Override
	public <X> ResultSetOutput<X> asResultSetOutput(Class<X> resultType) {
		if ( resultSetMapping == null
				|| (resultSetMapping.isDynamic() && resultSetMapping.getNumberOfResultBuilders() == 0) ) {
			resultSetMapping = Util.makeResultSetMapping(
					null,
					resultType,
					null,
					() -> sessionFactory
			);
		}
		else if ( resultSetMapping.getNumberOfResultBuilders() == 1 ) {
			var resultBuilder = resultSetMapping.getResultBuilders().get( 0 );
			var outputJavaType = resultBuilder.getJavaType();
			if ( outputJavaType != null && !resultType.isAssignableFrom( outputJavaType ) ) {
				throw new TypeMismatchException( String.format( Locale.ROOT,
						"Output type [%s] cannot be assigned to requested type [%s]",
						outputJavaType.getName(),
						resultType.getName()
				) );
			}
		}

		//noinspection unchecked
		return (ResultSetOutput<X>) this;
	}

	@Override
	public <X> ResultSetOutput<X> asResultSetOutput(jakarta.persistence.sql.ResultSetMapping<X> japMMapping) {
		this.resultSetMapping = JpaMappingHelper.toHibernateMapping( japMMapping, sessionFactory );
		//noinspection unchecked
		return (ResultSetOutput<X>) this;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Request the exact type the mapping produces, or one of its supertypes
  2. Align the result class passed to createStoredProcedureCall(name, resultClass) with what the @SqlResultSetMapping actually returns
  3. For DTO projections, define a @SqlResultSetMapping with @ConstructorResult(targetClass = MyDto.class) and request MyDto.class
  4. If the shape is uncertain, request Object[].class or Tuple instead of a concrete class

Example fix

// before
List<OrderReport> rows = outputs.getCurrent().as(OrderReport.class).getResultList(); // mapping yields Order entity

// after
List<Order> rows = outputs.getCurrent().as(Order.class).getResultList();
// or add: @SqlResultSetMapping(name="orderReport",
//   classes = @ConstructorResult(targetClass = OrderReport.class, columns = {...}))
Defensive patterns

Strategy: type-guard

Type guard

static <X> boolean resultTypeSafe(Class<?> mappedType, Class<X> requested) {
    return requested.isAssignableFrom(mappedType);
}

Try / catch

try {
    List<Order> rows = outputs.getCurrent().as(Order.class).getResultList();
} catch (org.hibernate.TypeMismatchException e) {
    // requested type is incompatible with the mapping: request the mapped type or fix the @SqlResultSetMapping
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/e854cd5763c8f9b9. Report an issue: GitHub.