{"record":{"id":"4023a8cca2bfb7a7","repo":"hibernate/hibernate-orm","slug":"identity-generation-requires-a-column","errorCode":null,"errorMessage":"Identity generation requires a column","messagePattern":"Identity generation requires a column","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java","lineNumber":457,"sourceCode":"\t\t\t\tsetNullValueUndefined();\n\t\t\t}\n\t\t\treturn generator;\n\t\t}\n\t\telse {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t@Internal\n\tpublic void setColumnToIdentity() {\n\t\tif ( getColumnSpan() != 1 ) {\n\t\t\tthrow new MappingException( \"Identity generation requires exactly one column\" );\n\t\t}\n\t\telse if ( getColumn(0) instanceof Column column ) {\n\t\t\tcolumn.setIdentity( true );\n\t\t}\n\t\telse {\n\t\t\tthrow new MappingException( \"Identity generation requires a column\" );\n\t\t}\n\t}\n\n\t@Override\n\tpublic boolean isUpdateable() {\n\t\t//needed to satisfy KeyValue\n\t\treturn true;\n\t}\n\n\t@Override\n\tpublic FetchStyle getFetchStyle() {\n\t\treturn FetchStyle.SELECT;\n\t}\n\n\t@Override\n\tpublic Table getTable() {\n\t\treturn table;\n\t}","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java#L439-L475","documentation":"Hibernate throws this MappingException from SimpleValue.setColumnToIdentity() when an identity id generator is applied to a value whose single column slot is not an actual Column (e.g. a Formula or some other KeyValue column object). Identity generation relies on the database auto-increment feature, so it needs exactly one real, physical column it can mark as IDENTITY. A formula or virtual column cannot be auto-generated by the database.","triggerScenarios":"Applying @GeneratedValue(strategy = GenerationType.IDENTITY) (or <generator class=\"identity\"/>) to an id whose SimpleValue maps a formula, a <formula> element in hbm.xml, or a column object that is not org.hibernate.mapping.Column; also programmatic models where a non-Column value is set as the single column of an identity-generated SimpleValue.","commonSituations":"hbm.xml mappings that mix <column> and <formula> on the id property; legacy mappings migrated between Hibernate versions where the id column was replaced by a formula; programmatic (boot) model builders that assign a virtual column to an identity id; dialects that force identity generation while the mapping declares a computed key.","solutions":["Remove any <formula> or virtual/non-Column definition from the identifier property so it maps exactly one physical column.","If the key really is computed, switch the generator away from IDENTITY (e.g. to @GeneratedValue(strategy = GenerationType.SEQUENCE) or no generator) since a formula cannot be identity-generated.","In hbm.xml, verify the <id> element contains only a single <column> child and no <formula> child.","For programmatic models, assert SimpleValue.getColumn(0) is an org.hibernate.mapping.Column before the generator is bound."],"exampleFix":"// before (hbm.xml)\n<id name=\"id\">\n    <formula>NEXT_VALUE('seq')</formula>\n    <generator class=\"identity\"/>\n</id>\n\n// after\n<id name=\"id\">\n    <column name=\"id\"/>\n    <generator class=\"native\"/>\n</id>","handlingStrategy":"validation","validationCode":"// before binding an identity generator to a programmatic SimpleValue\nif ( value.getColumnSpan() == 1 && value.getColumn(0) instanceof Column col ) {\n    value.setColumnToIdentity(); // safe\n}\nelse {\n    throw new IllegalStateException(\"id is not backed by exactly one physical Column; cannot use IDENTITY\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    metadata.buildMetadata().buildSessionFactory();\n} catch (MappingException e) {\n    if (e.getMessage().contains(\"Identity generation requires\")) {\n        throw new IllegalStateException(\"ID mapping must map exactly one physical column (no formulas) for IDENTITY generation: \" + e.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Never combine <formula> with identity generators on id properties.","For programmatic mappings, assert getColumn(0) instanceof Column before applying identity.","Build the SessionFactory in a CI smoke test so mapping errors surface at build time."],"tags":["hibernate","orm-mapping","id-generation","identity-column"],"backgroundTag":"invalid-orm-mapping","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}