{"record":{"id":"e8d3b24b6066176c","repo":"hibernate/hibernate-orm","slug":"could-not-format-discriminator-value-to-sql-string","errorCode":null,"errorMessage":"Could not format discriminator value to SQL string","messagePattern":"Could not format discriminator value to SQL string","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/persister/entity/DiscriminatorHelper.java","lineNumber":122,"sourceCode":"\t}\n\n\tprivate static <T> String discriminatorSqlLiteral(\n\t\t\tBasicType<T> discriminatorType,\n\t\t\tPersistentClass persistentClass,\n\t\t\tDialect dialect) {\n\t\treturn jdbcLiteral(\n\t\t\t\tdiscriminatorType.getJavaTypeDescriptor().fromString( persistentClass.getDiscriminatorValue() ),\n\t\t\t\tdiscriminatorType.getJdbcLiteralFormatter(),\n\t\t\t\tdialect\n\t\t);\n\t}\n\n\tpublic static <T> String jdbcLiteral(T value, JdbcLiteralFormatter<T> formatter, Dialect dialect) {\n\t\ttry {\n\t\t\treturn formatter.toJdbcLiteral( value, dialect, null );\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new MappingException( \"Could not format discriminator value to SQL string\", e );\n\t\t}\n\t}\n\n\t/**\n\t * Utility that computes the node type used in entity or embeddable type literals. Resolves to\n\t * either the {@link org.hibernate.metamodel.mapping.DiscriminatorType}, for polymorphic\n\t * domain types, or to {@link StandardBasicTypes#CLASS Class} for non-inherited ones.\n\t */\n\tpublic static <T> SqmBindableType<? super T> getDiscriminatorType(\n\t\t\tSqmPathSource<T> domainType, NodeBuilder nodeBuilder) {\n\t\tfinal SqmPathSource<?> subPathSource = domainType.findSubPathSource( DISCRIMINATOR_ROLE_NAME );\n\t\tfinal SqmBindableType<?> type = subPathSource != null\n\t\t\t\t? subPathSource.getPathType()\n\t\t\t\t: nodeBuilder.getTypeConfiguration().getBasicTypeRegistry().resolve( StandardBasicTypes.CLASS );\n\t\t//noinspection unchecked\n\t\treturn (SqmBindableType<? super T>) type;\n\t}\n}","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/persister/entity/DiscriminatorHelper.java#L104-L140","documentation":"DiscriminatorHelper.jdbcLiteral renders a discriminator value as a SQL literal for the active dialect by delegating to the discriminator type's JdbcLiteralFormatter; any exception from the formatter is wrapped in MappingException('Could not format discriminator value to SQL string'). The literal parsed fine but could not be emitted — typically a custom or unusual discriminator Java/JDBC type whose formatter rejects the value or lacks dialect support. Reached via getDiscriminatorSQLValue() during persister construction.","triggerScenarios":"A discriminator mapped with a custom BasicType/JavaType whose JdbcLiteralFormatter.toJdbcLiteral() throws for the mapped value; enum/char discriminators whose rendering path is unsupported on the target dialect; boot of the factory against a dialect the custom type never handled.","commonSituations":"Custom discriminator types introduced for legacy schemas; switching databases/dialects where literal rendering differs; values that parse via fromString() but cannot be rendered back.","solutions":["Use a standard discriminator type (STRING/INTEGER/CHAR) via @DiscriminatorColumn","Fix the custom type: implement/override JdbcLiteralFormatter so toJdbcLiteral() handles the discriminator value for every dialect you deploy on","Add boot-time tests with each target dialect to catch formatter failures before deployment"],"exampleFix":"// before: custom type on the discriminator column lacking JdbcLiteralFormatter support\n@DiscriminatorColumn(name = \"kind\", discriminatorType = STRING) // + XML applies custom type\n\n// after: plain basic discriminator type with built-in formatter\n@DiscriminatorColumn(name = \"kind\", discriminatorType = STRING)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    SessionFactory sf = metadata.getSessionFactoryBuilder().build();\n}\ncatch ( org.hibernate.MappingException e ) {\n    // discriminator literal could not be rendered for the dialect\n    throw new IllegalStateException(\"SessionFactory boot failed on dialect \" + dialect + \": \" + e.getMessage(), e);\n}","preventionTips":["If you must use a custom discriminator type, implement and unit-test its JdbcLiteralFormatter for every production dialect","Boot the factory in CI against every target database/dialect","Prefer built-in discriminator types (STRING, INTEGER, CHAR)"],"tags":["hibernate","orm","discriminator","sql","dialect","custom-type"],"backgroundTag":"jdbc-literal-format-error","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}