{"record":{"id":"113a0669cf0a8f3b","repo":"hibernate/hibernate-orm","slug":"enum-literal-cannot-be-cast-to-biginteger","errorCode":null,"errorMessage":"Enum literal cannot be cast to BigInteger","messagePattern":"Enum literal cannot be cast to BigInteger","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/expression/SqmEnumLiteral.java","lineNumber":172,"sourceCode":"\t\treturn nodeBuilder().literal( ordinalValue().floatValue() );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic SqmExpression<Double> asDouble() {\n\t\treturn nodeBuilder().literal( ordinalValue().doubleValue() );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic SqmExpression<BigDecimal> asBigDecimal() {\n\t\tthrow new UnsupportedOperationException( \"Enum literal cannot be cast to BigDecimal\" );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic SqmExpression<BigInteger> asBigInteger() {\n\t\tthrow new UnsupportedOperationException( \"Enum literal cannot be cast to BigInteger\" );\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic SqmExpression<String> asString() {\n\t\treturn nodeBuilder().literal( getExpressibleJavaType().toName( getEnumValue() ) );\n\t}\n\n\t@Override\n\tpublic <X> X accept(SemanticQueryWalker<X> walker) {\n\t\treturn walker.visitEnumLiteral( this );\n\t}\n\n\t@Override\n\tpublic void appendHqlString(StringBuilder hql, SqmRenderContext context) {\n\t\thql.append( getEnumValue().getDeclaringClass().getTypeName() );\n\t\thql.append( '.' );\n\t\thql.append( enumValueName );","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/expression/SqmEnumLiteral.java#L154-L190","documentation":"Thrown by SqmEnumLiteral.asBigInteger(). The SQM enum literal supports asInteger/asLong/asFloat/asDouble (ordinal-based) and asString (name-based), but BigInteger conversion is not implemented because an enum constant has no integer magnitude beyond its ordinal, so the override unconditionally throws UnsupportedOperationException.","triggerScenarios":"Calling asBigInteger() on an expression Hibernate resolved to an SqmEnumLiteral: criteria code like builder.literal(MyEnum.STATUS_A).asBigInteger(), generic coercion code that calls asBigInteger() on every expression node, or casting/forcing an enum literal into a BigInteger-typed comparison.","commonSituations":"Generic numeric-coercion utilities applied to all operands; ID/score values modeled as enums but used in BigInteger arithmetic; code migrated from hand-written SQL where the enum was stored as a number and compared to BigInteger parameters.","solutions":["Convert through a supported type first: asInteger() (ordinal) or asString() (name), then cast - e.g. asInteger().cast(BigInteger.class).","Compare against the ordinal or name directly instead of coercing the literal.","Store the numeric magnitude as a real attribute (or map via AttributeConverter to BigInteger) and query that instead of the enum literal.","Branch in generic code: skip asBigInteger() when the expression is an SqmEnumLiteral."],"exampleFix":"// before\nSqmExpression<BigInteger> v = enumLiteralExpr.asBigInteger(); // UnsupportedOperationException\n\n// after - via ordinal, then cast\nSqmExpression<BigInteger> v = enumLiteralExpr.asInteger().cast( BigInteger.class );","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isEnumLiteral(SqmExpression<?> e) {\n    return e instanceof org.hibernate.query.sqm.tree.spi.expression.SqmEnumLiteral;\n}","tryCatchPattern":"try {\n    return expr.asBigInteger();\n} catch (UnsupportedOperationException e) {\n    // enum literal: fall back to an ordinal-based cast\n    return expr.asInteger().cast( BigInteger.class );\n}","preventionTips":["Never call asBigInteger()/asBigDecimal() on enum-typed expressions; use asInteger() (ordinal) or asString() (name) first.","Skip BigInteger coercion for SqmEnumLiteral nodes in generic builders.","Model numeric magnitudes as real numeric attributes rather than enum constants."],"tags":["hibernate","sqm","enum","biginteger","cast","criteria-api"],"backgroundTag":"unsupported-cast-operation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}