hibernate/hibernate-orm · error · IllegalArgumentException

Requested tuple value [alias=%s, value=%s] cannot be assigne

Error message

Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]

What it means

Thrown by TupleImpl.get(String alias, Class type): the alias resolved fine, but the stored value's runtime type is not an instance of the requested type. The message prints the alias, the actual value, and the requested class FQN, so the mismatch is directly readable. Null values skip the check and are returned as null.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/internal/TupleImpl.java:48

	@Override
	public <X> X get(TupleElement<X> tupleElement) {
		final Integer index = tupleMetadata.get( tupleElement );
		if ( index == null ) {
			throw new IllegalArgumentException(
					"Requested tuple element did not correspond to element in the result tuple"
			);
		}
		// index should be "in range" by nature of size check in ctor
		return cast( tupleElement.getJavaType(), row[index] );
	}

	@Override
	public <X> X get(String alias, Class<X> type) {
		final Object untyped = get( alias );
		if ( untyped != null ) {
			if ( !isInstance( type, untyped ) ) {
				throw new IllegalArgumentException(
						String.format(
								"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]",
								alias,
								untyped,
								type.getName()
						)
				);
			}
		}
		return cast( type, untyped );
	}

	@Override
	public Object get(String alias) {
		final Integer index = tupleMetadata.get( alias );
		if ( index == null ) {
			throw new IllegalArgumentException(
					"Given alias [" + alias + "] did not correspond to an element in the result tuple"

View on GitHub (pinned to fad1729dce)

Solutions

  1. Request the type Hibernate actually produces (Long for ids/counts), or a common supertype (`Number.class`) and convert yourself
  2. Fix the select expression type: `cast(count(e) as integer)` in HQL
  3. After any schema or dialect change, dump `tuple.get(alias).getClass()` once to re-learn the runtime types

Example fix

// before
int count = tuple.get("cnt", Integer.class);   // Long stored
// after
long count = tuple.get("cnt", Long.class);
Defensive patterns

Strategy: type-guard

Validate before calling

// Check assignability before the typed get
Object raw = tuple.get(alias);
Class<?> want = Long.class;
if (raw != null && !want.isInstance(raw)) { /* read as Number and convert */ }

Type guard

static <X> X tupleGet(Tuple t, String alias, Class<X> type) {
    Object v = t.get(alias);
    if (v == null) return null;
    if (type.isInstance(v)) return type.cast(v);
    if (v instanceof Number n && type == Integer.class) return type.cast(n.intValue());
    if (v instanceof Number n && type == Long.class) return type.cast(n.longValue());
    throw new ClassCastException(v.getClass() + " -> " + type);
}

Try / catch

catch (IllegalArgumentException e) { if (e.getMessage().contains("cannot be assigned to requested type")) { /* read as Number.class and convert */ } throw e; }

Prevention

When it happens

Trigger: `tuple.get("count", Integer.class)` when the database/Hibernate returns Long for count aggregates; `tuple.get("id", Integer.class)` for an id mapped as Long; requesting a specific Number subclass (Integer/BigDecimal) different from what the dialect returns.

Common situations: Assuming Integer for SQL counts and ids that Hibernate maps as Long; dialect differences in numeric type resolution between test and production databases; requesting a value type that changed after a schema or mapping change.

Related errors


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