{"record":{"id":"14e61441a951a214","repo":"hibernate/hibernate-orm","slug":"emulation-of-function-chr-supports-only-integer","errorCode":null,"errorMessage":"Emulation of function chr() supports only integer literals, but %s argument given","messagePattern":"Emulation of function chr\\(\\) supports only integer literals, but (.+?) argument given","errorType":"exception","errorClass":"QueryException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/dialect/function/ChrLiteralEmulation.java","lineNumber":48,"sourceCode":"/**\n * A chr implementation that translates integer literals to string literals.\n *\n * @author Christian Beikov\n */\npublic class ChrLiteralEmulation extends AbstractSqmSelfRenderingFunctionDescriptor {\n\n\tpublic ChrLiteralEmulation(TypeConfiguration typeConfiguration) {\n\t\tsuper(\n\t\t\t\t\"chr\",\n\t\t\t\tnew ArgumentTypesValidator(\n\t\t\t\t\t\tStandardArgumentsValidators.composite(\n\t\t\t\t\t\t\t\tStandardArgumentsValidators.exactly(1),\n\t\t\t\t\t\t\t\tnew ArgumentsValidator() {\n\t\t\t\t\t\t\t\t\t@Override\n\t\t\t\t\t\t\t\t\tpublic void validate(List<? extends SqmTypedNode<?>> arguments, String functionName, BindingContext bindingContext) {\n\t\t\t\t\t\t\t\t\t\tfinal var arg = arguments.get( 0 );\n\t\t\t\t\t\t\t\t\t\tif ( !( arg instanceof SqmLiteral<?> ) ) {\n\t\t\t\t\t\t\t\t\t\t\tthrow new QueryException(\n\t\t\t\t\t\t\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\"Emulation of function chr() supports only integer literals, but %s argument given\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\targ.getClass().getName()\n\t\t\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t),\n\t\t\t\t\t\tINTEGER\n\t\t\t\t),\n\t\t\t\tStandardFunctionReturnTypeResolvers.invariant(\n\t\t\t\t\t\ttypeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.CHARACTER )\n\t\t\t\t),\n\t\t\t\tStandardFunctionArgumentTypeResolvers.invariant( typeConfiguration, INTEGER )\n\t\t);\n\t}","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/dialect/function/ChrLiteralEmulation.java#L30-L66","documentation":"ChrLiteralEmulation is registered by dialects that lack a native chr() function; it rewrites chr(<int literal>) into a char literal during query compilation. Because the rewrite needs the actual integer at compile time, passing anything that is not an SqmLiteral (parameter, column, expression) throws a QueryException naming the argument's class name.","triggerScenarios":"Using chr() on a dialect that registers the emulation with a non-literal argument: 'from E where e.code = chr(:code)', chr(e.col), chr(65+1), or criteria builder chr( parameter ). Only a constant like chr(65) is accepted.","commonSituations":"Porting queries from PostgreSQL/Oracle (native chr) to a database whose dialect emulates chr; dynamic queries building chr() around user input parameters; test code running against H2/other DB whose dialect chain enables the emulation.","solutions":["Pass a constant integer literal: chr(65), or precompute the character in Java and use a plain char/String parameter","Replace chr() with the equivalent HQL/JPQL: use a bind parameter of type Character/String instead of building characters in SQL","If you need runtime chr(), override the dialect function with a native or SQL-based implementation (e.g. char() on MySQL/SAP HANA) that accepts expressions"],"exampleFix":"// before\nsession.createQuery(\"from Code c where c.ch = chr(:n)\").setParameter(\"n\", 65);\n\n// after\nsession.createQuery(\"from Code c where c.ch = :ch\").setParameter(\"ch\", 'A'); // resolved in Java","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// In Java: guard that chr() only ever receives compile-time constants\nboolean chrSafe(Object arg) { return arg instanceof Integer i && isQueryLiteral(i); }","tryCatchPattern":"catch (QueryException e) {\n    if (e.getMessage().contains(\"chr()\")) {\n        // fall back to binding the character as a parameter\n        return session.createQuery(\"from Code c where c.ch = :ch\", Code.class)\n                      .setParameter(\"ch\", (char) code);\n    }\n    throw e;\n}","preventionTips":["Prefer binding Characters/Strings from Java over generating chr() in HQL","Ban chr() in shared HQL snippets; keep a project HQL style guide per dialect","When porting SQL, replace chr(n) with the literal character in constants"],"tags":["hibernate","hql","chr-function","function-emulation","literal-required"],"backgroundTag":"hql-function-argument-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}