{"record":{"id":"4289d5e5235d0ecf","repo":"hibernate/hibernate-orm","slug":"expecting-column-for-collection-id-idbag-but-fo","errorCode":null,"errorMessage":"Expecting column for collection id (idbag), but found formula [%s.%s]","messagePattern":"Expecting column for collection id \\(idbag\\), but found formula \\[(.+?)\\.(.+?)\\]","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeSourceIdBagImpl.java","lineNumber":75,"sourceCode":"\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic SizeSource getSizeSource() {\n\t\t\t\t\t\treturn Helper.interpretSizeSource(\n\t\t\t\t\t\t\t\tidBagMapping.getCollectionId().getLength(),\n\t\t\t\t\t\t\t\t(Integer) null,\n\t\t\t\t\t\t\t\tnull\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\t@Override\n\t\t\t\t\tpublic List<JaxbHbmColumnType> getColumnOrFormulaElements() {\n\t\t\t\t\t\treturn idBagMapping.getCollectionId().getColumn();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t);\n\n\t\tif ( !(collectionIdRelationalValueSource instanceof ColumnSource) ) {\n\t\t\tthrow new MappingException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ENGLISH,\n\t\t\t\t\t\t\t\"Expecting column for collection id (idbag), but found formula [%s.%s]\",\n\t\t\t\t\t\t\tcontainer.getAttributeRoleBase().getFullPath(),\n\t\t\t\t\t\t\tidBagMapping.getName()\n\t\t\t\t\t),\n\t\t\t\t\tsourceMappingDocument().getOrigin()\n\t\t\t);\n\t\t}\n\n\n\t\tthis.collectionIdSource = new CollectionIdSourceImpl(\n\t\t\t\t(ColumnSource) collectionIdRelationalValueSource,\n\t\t\t\tnew HibernateTypeSourceImpl( idBagMapping.getCollectionId().getType() ),\n\t\t\t\tidBagMapping.getCollectionId().getGenerator().getClazz(),\n\t\t\t\tHelper.extractParameters( idBagMapping.getCollectionId().getGenerator().getConfigParameters() )\n\t\t);\n\t}","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/PluralAttributeSourceIdBagImpl.java#L57-L93","documentation":"An <idbag> collection derives row identity from <collection-id>, and that id must map to a physical column. PluralAttributeSourceIdBagImpl builds the relational value source for the collection id and requires a ColumnSource; a formula resolves to a DerivedValueSource instead, so bootstrap fails with this message naming the containing attribute role and the idbag name.","triggerScenarios":"An <idbag> whose <collection-id> declares its value via a formula - either a formula='...' style attribute or a nested <formula> element - instead of a <column> element.","commonSituations":"Copy-pasting a formula-based property mapping into <collection-id>; assuming a computed expression (e.g. a sequence call) can serve as the collection's surrogate id; tool-generated mappings that emit <formula> uniformly.","solutions":["Declare a real column with a generator, e.g. <collection-id type='long'><column name='item_id'/><generator class='increment'/></collection-id>","Add the corresponding physical column to the collection table","If no surrogate id is needed, use <bag> instead of <idbag>"],"exampleFix":"// before\n<idbag name='items' table='item'>\n    <collection-id type='long'>\n        <formula>nextval('item_seq')</formula>\n    </collection-id>\n    ...\n</idbag>\n\n// after\n<idbag name='items' table='item'>\n    <collection-id type='long'>\n        <column name='item_id'/>\n        <generator class='increment'/>\n    </collection-id>\n    ...\n</idbag>","handlingStrategy":"validation","validationCode":"// Pre-flight: <collection-id> must contain a <column>, never a <formula>\nvar cid = (org.w3c.dom.Element) doc.getElementsByTagName(\"collection-id\").item(0);\nif (cid.getElementsByTagName(\"formula\").getLength() > 0 || !cid.getAttribute(\"formula\").isEmpty())\n    throw new IllegalStateException(\"idbag collection-id must be a physical column\");","typeGuard":null,"tryCatchPattern":"catch (org.hibernate.boot.MappingException e) at Metadata build: the message names the idbag attribute role - open that <idbag> and replace the formula under <collection-id> with a <column> plus generator","preventionTips":["Give idbag collection tables a real surrogate id column backed by a generator","Never copy formula-based property mappings into <collection-id>","If the collection needs no surrogate id, map it as <bag> instead of <idbag>"],"tags":["hibernate","hbm","idbag","collection-id","formula","orm"],"backgroundTag":"formula-instead-of-column","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}