hibernate/hibernate-orm · error · InstantiationException

Cannot instantiate query result type, argument types are unk

Error message

Cannot instantiate query result type, argument types are unknown 

What it means

Thrown while building RowTransformerConstructorImpl: the query has exactly one selected element and Hibernate could not resolve a Java type for it (resolveElementJavaType returned null because the SqmExpressible has no expressible Java type and TupleElement.getJavaType() is null), so a matching constructor cannot even be searched for. In the standard SQM path this InstantiationException is caught at ConcreteSqmSelectQueryPlan:371 and replaced with the checking transformer, so it usually re-surfaces later as the QueryTypeMismatchException of error 3186 rather than in this raw form.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowTransformerConstructorImpl.java:41

 * @author Gavin King
 */
public class RowTransformerConstructorImpl<T> implements RowTransformer<T> {
	private final Class<T> type;
	private final Constructor<T> constructor;

	public RowTransformerConstructorImpl(
			Class<T> type,
			TupleMetadata tupleMetadata,
			TypeConfiguration typeConfiguration) {
		this.type = type;
		assert tupleMetadata != null : "TupleMetadata must not be null";
		final List<TupleElement<?>> elements = tupleMetadata.getList();
		final List<Class<?>> argumentTypes = elements.stream()
				.map( RowTransformerConstructorImpl::resolveElementJavaType )
				.collect( toList() );
		if ( argumentTypes.size() == 1 && argumentTypes.get( 0 ) == null ) {
			// Can not (properly) resolve constructor for single null element
			throw new InstantiationException( "Cannot instantiate query result type, argument types are unknown ", type );
		}

		constructor = findMatchingConstructor( type, argumentTypes, typeConfiguration );
		if ( constructor == null ) {
			throw new InstantiationException( "Cannot instantiate query result type, found no matching constructor", type );
		}
		constructor.setAccessible( true );
	}

	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();

View on GitHub (pinned to fad1729dce)

Solutions

  1. Select a typed expression: `select cast(null as string) from Employee e`, or select a real column/function with a known type
  2. If the value genuinely has no type, query with `Object.class` (which skips constructor transformation) instead of a bean class
  3. Give the expression an explicit type via `cast(... as <type>)` in HQL so constructor lookup can proceed

Example fix

// before
List<Foo> r = session.createQuery("select null from Employee e", Foo.class).getResultList();
// after
List<String> r = session.createQuery("select cast(null as string) from Employee e", String.class).getResultList();
Defensive patterns

Strategy: validation

Validate before calling

// Avoid untyped single selections; make the type explicit before requesting a bean result type
String hql = "select cast(null as string) from Employee e"; // typed placeholder instead of bare `select null`

Prevention

When it happens

Trigger: `select null from Employee e` combined with `SomeClass.class`; a single selection of an expression whose type cannot be determined (untyped native SQL fragment, untyped parameter); requesting a bean result type for a single untyped column.

Common situations: Prototyping with `select null` placeholders; queries over database functions or fragments Hibernate cannot type-resolve; migrations where a selection used to be typed and now resolves to null type.

Related errors


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