{"record":{"id":"128c7a2e3b8f5285","repo":"hibernate/hibernate-orm","slug":"expecting-single-column-in-context-of-s-name-s","errorCode":null,"errorMessage":"Expecting single column in context of <%s name=\"%s\"/>, but found formula [%s]","messagePattern":"Expecting single column in context of <(.+?) name=\"(.+?)\"/>, but found formula \\[(.+?)\\]","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalValueSourceHelper.java","lineNumber":190,"sourceCode":"\t\t\tMappingDocument mappingDocument,\n\t\t\tString containingTableName,\n\t\t\tColumnsAndFormulasSource columnsAndFormulasSource) {\n\t\tfinal List<RelationalValueSource> sources = buildValueSources(\n\t\t\t\tmappingDocument,\n\t\t\t\tcontainingTableName,\n\t\t\t\tcolumnsAndFormulasSource\n\t\t);\n\n\t\tif ( sources.size() > 1 ) {\n\t\t\tthrow new MappingException( multipleError( columnsAndFormulasSource ), mappingDocument.getOrigin() );\n\t\t}\n\n\t\tfinal RelationalValueSource result = sources.get( 0 );\n\t\tif ( result instanceof ColumnSource columnSource ) {\n\t\t\treturn columnSource;\n\t\t}\n\t\telse {\n\t\t\tthrow new MappingException( formulaError( columnsAndFormulasSource, (DerivedValueSource) result ),\n\t\t\t\t\tmappingDocument.getOrigin() );\n\t\t}\n\t}\n\n\t/**\n\t * Given a {@link ColumnsAndFormulasSource}, build the corresponding list of\n\t * {@link ColumnSource}.  Any formula, rather than a column, will result in an exception.\n\t *\n\t * @param mappingDocument the mapping document\n\t * @param containingTableName The logical name of the table containing the relational values\n\t * @param columnsAndFormulasSource the adapter describing the value sources.\n\t *\n\t * @return The corresponding list.\n\t */\n\tpublic static List<ColumnSource> buildColumnSources(\n\t\t\tMappingDocument mappingDocument,\n\t\t\tString containingTableName,\n\t\t\tColumnsAndFormulasSource columnsAndFormulasSource) {","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalValueSourceHelper.java#L172-L208","documentation":"After buildColumnSource narrows a mapping to exactly one relational value, that value must be a ColumnSource. If the single source is a DerivedValueSource - a <formula> element or formula='...' attribute - Hibernate throws this message echoing the element, its name and the formula expression. The mapping position requires a physical column; derived values are not accepted here (unlike regular <property> mappings).","triggerScenarios":"Supplying a formula where exactly one column is required: e.g. a <version formula='...'/> or <collection-id> built through a <formula> element on a mapping handled by buildColumnSource. The printed formula text is the derived expression that was found.","commonSituations":"Wanting a computed discriminator or version value; migrating a mapping that used a formula where the current Hibernate version now demands a real column; not realizing which elements accept formulas (plain <property> does, these single-column elements do not).","solutions":["Replace the formula with a physical column: <column name='...'/> backed by a real table column","If you also need the computed value, map it as a separate read-only <property formula='...'> alongside the required column","Check the Hibernate version's support for formula-based discriminators; if unsupported, use a real discriminator column"],"exampleFix":"// before\n<discriminator formula=\"case when t = 'A' then 'TYPE_A' else 'TYPE_B' end\"/>\n\n// after\n<discriminator column='dtype'/>","handlingStrategy":"validation","validationCode":"// Pre-flight: column-required elements must not carry a formula\nvar e = columnRequiredElement; // e.g. <version>, <collection-id>, <discriminator>\nboolean hasFormula = e.getElementsByTagName(\"formula\").getLength() > 0 || !e.getAttribute(\"formula\").isEmpty();\nif (hasFormula)\n    throw new IllegalStateException(e.getTagName() + \" requires a physical column, formulas are not allowed\");","typeGuard":null,"tryCatchPattern":"catch (org.hibernate.boot.MappingException e) at Metadata build: the message echoes the offending formula - replace it with a real column and, if still needed, map the expression as a separate read-only <property formula='...'>","preventionTips":["Learn which positions accept formulas (plain <property>) and which do not (version, timestamp, collection-id, keys)","Add computed-value properties separately instead of overloading single-column elements","Bootstrap the SessionFactory in tests to catch formula misuse before deployment"],"tags":["hibernate","hbm","formula","mapping","columns","orm"],"backgroundTag":"formula-instead-of-column","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}