{"record":{"id":"f4711756bdcf033f","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-value-s-s-to-long","errorCode":null,"errorMessage":"Cannot coerce value '%s' [%s] to Long","messagePattern":"Cannot coerce value '(.+?)' \\[(.+?)\\] to Long","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongJavaType.java","lineNumber":129,"sourceCode":"\t@Override\n\tpublic boolean isWider(JavaType<?> javaType) {\n\t\treturn switch ( javaType.getTypeName() ) {\n\t\t\tcase\n\t\t\t\t\"byte\", \"java.lang.Byte\",\n\t\t\t\t\"short\", \"java.lang.Short\",\n\t\t\t\t\"int\", \"java.lang.Integer\" -> true;\n\t\t\tdefault -> false;\n\t\t};\n\t}\n\n\t@Override\n\tpublic @Nullable Long coerce(@Nullable Object value) {\n\t\tif ( value == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tfinal var coerced = coerceOrNull(  value );\n\t\tif (coerced == null) {\n\t\t\tthrow new CoercionException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Cannot coerce value '%s' [%s] to Long\",\n\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\tvalue.getClass().getName()\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\t\treturn coerced;\n\t}\n\n\t@Override\n\tpublic @Nullable Long coerceOrNull(@Nonnull Object value) {\n\t\tif ( value instanceof Long longValue ) {\n\t\t\treturn longValue;\n\t\t}\n\n\t\tif ( value instanceof Byte byteValue ) {","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LongJavaType.java#L111-L147","documentation":"LongJavaType.coerce() is invoked when Hibernate must convert a value into a Long during query parameter binding or result coercion. It first tries coerceOrNull (numeric types, parseable numeric strings); when no conversion path exists it throws CoercionException naming the offending value and its class. The exception means the value's runtime type is fundamentally unconvertible, not merely out of range.","triggerScenarios":"query.setParameter(\"n\", someObject) where the parameter's implied type is Long but the argument is an enum, date, UUID or arbitrary object; native query columns mapped to Long attributes returning non-numeric types; criteria/HQL function results coerced to Long; AttributeConverter producing a type Hibernate cannot coerce","commonSituations":"Passing the wrong variable to a typed parameter after refactoring; native queries whose SELECT expression type differs from the mapped attribute; DB views where a column changed type; String values containing non-numeric text bound to Long parameters.","solutions":["Pass a numeric type or a numeric string that coerceOrNull can parse, or bind with an explicit TypedParameterValue/parameter type","For native queries, SELECT a numeric expression (CAST in SQL) or remap the attribute to the actual returned type","Validate/convert the value before binding: Number.longValue() or Long.parseLong after a regex check","If the source column legitimately varies, map it as String and convert in an AttributeConverter"],"exampleFix":"// before\nquery.setParameter(\"limit\", someRequestDto); // CoercionException: Cannot coerce value '...[com.acme.Dto]' to Long\n\n// after\nquery.setParameter(\"limit\", someRequestDto.getLimit()); // returns long/Long","handlingStrategy":"type-guard","validationCode":"static boolean coercibleToLong(Object v) {\n    if (v == null || v instanceof Number || v instanceof java.math.BigInteger) return true;\n    if (v instanceof String s) return s.matches(\"[+-]?\\d+\");\n    return false;\n}\n\nif (!coercibleToLong(param)) throw new IllegalArgumentException(\"Not coercible to Long: \" + param);","typeGuard":"static Long toLongOrNull(Object v) {\n    if (v instanceof Number n) return n.longValue();\n    if (v instanceof String s && s.matches(\"[+-]?\\d+\")) return Long.parseLong(s);\n    return null;\n}","tryCatchPattern":"try { Long bound = LongJavaType.INSTANCE.coerce(value); }\ncatch (CoercionException e) {\n    throw new IllegalArgumentException(\"Parameter is not numeric: cannot bind to Long\", e);\n}","preventionTips":["Bind values whose types match the parameter/attribute type exactly","Use TypedParameterValue for ambiguous native-query parameters","CAST expressions in native SQL so columns arrive numeric"],"tags":["hibernate","type-coercion","query-parameter","native-query","long"],"backgroundTag":"type-coercion-failure","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}