{"record":{"id":"68663f3495253818","repo":"hibernate/hibernate-orm","slug":"given-typedparametervalue-is-not-assignable-to-giv","errorCode":null,"errorMessage":"Given TypedParameterValue is not assignable to given Parameter type","messagePattern":"Given TypedParameterValue is not assignable to given Parameter type","errorType":"exception","errorClass":"QueryArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/internal/AbstractCommonQueryContract.java","lineNumber":1288,"sourceCode":"\t\t\t@Nullable P value,\n\t\t\t@Nonnull Type<P> type) {\n\t\tlocateBinding( parameter ).setBindValue( value, (BindableType<P>) type );\n\t\treturn this;\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <P> CommonQueryContractImplementor setParameter(\n\t\t\t@Nonnull Parameter<P> parameter,\n\t\t\t@Nullable P value) {\n\t\tif ( value instanceof TypedParameterValue<?> typedParameterValue ) {\n\t\t\tfinal var parameterType = parameter.getParameterType();\n\t\t\tfinal var type = typedParameterValue.type();\n\t\t\tif ( type == null ) {\n\t\t\t\tthrow new IllegalArgumentException( \"TypedParameterValue has no type\" );\n\t\t\t}\n\t\t\tif ( !parameterType.isAssignableFrom( type.getJavaType() ) ) {\n\t\t\t\tthrow new QueryArgumentException( \"Given TypedParameterValue is not assignable to given Parameter type\",\n\t\t\t\t\t\tparameterType, typedParameterValue.value() );\n\t\t\t}\n\t\t\t@SuppressWarnings(\"unchecked\") // safe, because we just checked\n\t\t\tfinal var typedValue = (TypedParameterValue<P>) value;\n\t\t\tfinal var castValue = typedValue.value();\n\t\t\tfinal var castType = typedValue.type();\n\t\t\tif ( parameter instanceof QueryParameter<P> queryParameter ) {\n\t\t\t\tsetParameter( queryParameter, castValue, castType );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tlocateBinding( parameter ).setBindValue( castValue, castType );\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tlocateBinding( parameter ).setBindValue( value, resolveJdbcParameterTypeIfNecessary() );\n\t\t}\n\t\treturn this;\n\t}","sourceCodeStart":1270,"sourceCodeEnd":1306,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractCommonQueryContract.java#L1270-L1306","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nquery.setParameter( param, new TypedParameterValue<>( StandardBasicTypes.STRING, \"1\" ) );\n// parameter type Long: String not assignable\n\n// after\nquery.setParameter( param, new TypedParameterValue<>( StandardBasicTypes.LONG, 1L ) );","handlingStrategy":"validation","validationCode":"TypedParameterValue<?> tpv = (TypedParameterValue<?>) value;\nClass<?> paramType = param.getParameterType();\nif ( tpv != null && tpv.type() != null && !paramType.isAssignableFrom( tpv.type().getJavaType() ) ) {\n    throw new IllegalArgumentException( \"Need \" + paramType + \", got \" + tpv.type().getJavaType() );\n}\nquery.setParameter( param, value );","typeGuard":"static boolean typedValueMatches(Parameter<?> p, TypedParameterValue<?> v) {\n    return v.type() != null && p.getParameterType().isAssignableFrom( v.type().getJavaType() );\n}","tryCatchPattern":"try {\n    query.setParameter( param, typedValue );\n} catch ( org.hibernate.query.QueryArgumentException e ) {\n    throw new IllegalArgumentException( \"Param expects \" + param.getParameterType()\n            + \", got TypedParameterValue of \" + typedValue.type().getJavaType(), e );\n}","preventionTips":["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"],"tags":["hibernate","query-parameters","typed-parameter-value","type-mismatch","query-argument-exception"],"backgroundTag":"query-parameter-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}