hibernate/hibernate-orm · error · QueryArgumentException
Given TypedParameterValue is not assignable to given Paramet
Error message
Given TypedParameterValue is not assignable to given Parameter type
What it means
Thrown (QueryArgumentException) by setParameter(Parameter<P>, P value) when the TypedParameterValue's type is present but its Java type is not assignable to the parameter's declared type: !parameterType.isAssignableFrom(type.getJavaType()). The exception carries the parameter type and the typed value, so you can inspect both sides of the mismatch.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/query/internal/AbstractCommonQueryContract.java:1288
@Nullable P value,
@Nonnull Type<P> type) {
locateBinding( parameter ).setBindValue( value, (BindableType<P>) type );
return this;
}
@Override
@Nonnull
public <P> CommonQueryContractImplementor setParameter(
@Nonnull Parameter<P> parameter,
@Nullable P value) {
if ( value instanceof TypedParameterValue<?> typedParameterValue ) {
final var parameterType = parameter.getParameterType();
final var type = typedParameterValue.type();
if ( type == null ) {
throw new IllegalArgumentException( "TypedParameterValue has no type" );
}
if ( !parameterType.isAssignableFrom( type.getJavaType() ) ) {
throw new QueryArgumentException( "Given TypedParameterValue is not assignable to given Parameter type",
parameterType, typedParameterValue.value() );
}
@SuppressWarnings("unchecked") // safe, because we just checked
final var typedValue = (TypedParameterValue<P>) value;
final var castValue = typedValue.value();
final var castType = typedValue.type();
if ( parameter instanceof QueryParameter<P> queryParameter ) {
setParameter( queryParameter, castValue, castType );
}
else {
locateBinding( parameter ).setBindValue( castValue, castType );
}
}
else {
locateBinding( parameter ).setBindValue( value, resolveJdbcParameterTypeIfNecessary() );
}
return this;
}View on GitHub (pinned to fad1729dce)
Solutions
- Match the TypedParameterValue type to the parameter's declared Java type — inspect parameter.getParameterType() first and pick the corresponding StandardBasicTypes constant
- Fix on the query side if the declared type is wrong (cast in HQL, explicit type in mapping) instead of forcing a mismatched bind
- Add a debug assertion comparing type.getJavaType() against parameter.getParameterType() before binding
Example fix
// before query.setParameter( param, new TypedParameterValue<>( StandardBasicTypes.STRING, "1" ) ); // parameter type Long: String not assignable // after query.setParameter( param, new TypedParameterValue<>( StandardBasicTypes.LONG, 1L ) );
Defensive patterns
Strategy: validation
Validate before calling
TypedParameterValue<?> tpv = (TypedParameterValue<?>) value;
Class<?> paramType = param.getParameterType();
if ( tpv != null && tpv.type() != null && !paramType.isAssignableFrom( tpv.type().getJavaType() ) ) {
throw new IllegalArgumentException( "Need " + paramType + ", got " + tpv.type().getJavaType() );
}
query.setParameter( param, value ); Type guard
static boolean typedValueMatches(Parameter<?> p, TypedParameterValue<?> v) {
return v.type() != null && p.getParameterType().isAssignableFrom( v.type().getJavaType() );
} Try / catch
try {
query.setParameter( param, typedValue );
} catch ( org.hibernate.query.QueryArgumentException e ) {
throw new IllegalArgumentException( "Param expects " + param.getParameterType()
+ ", got TypedParameterValue of " + typedValue.type().getJavaType(), e );
} Prevention
- Pick the StandardBasicTypes constant that mirrors the parameter's inferred Java type
- Avoid blanket STRING/INTEGER wrapping in generic DAO helpers
- After entity type changes (Integer→Long etc.), grep for TypedParameterValue uses of the old type
When it happens
Trigger: Parameter declared as Long (query/criteria inference) but bound with new TypedParameterValue<>(StandardBasicTypes.STRING, "1"). Temporal mismatch: parameter expects java.time.Instant, TypedParameterValue supplies TimestampType. Enum vs String across a criteria/HQL boundary.
Common situations: Helper methods that wrap every bind value in TypedParameterValue using one generic type (e.g. always STRING) while queries infer specific types; migrating from java.sql.Timestamp to java.time types with old typed-value constants; DB schema change (column type change) altering inferred parameter types after a regeneration.
Related errors
- Argument to query parameter has an incompatible type
- Type specified for parameter named '{name}' is incompatible
- Type specified for parameter at position {position} is incom
- TypedParameterValue has no type
- jakarta.persistence.validation.group.{} is of unknown type:
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/68663f3495253818.
Report an issue: GitHub.