{"record":{"id":"b1ec4f7cc47b6ae5","repo":"hibernate/hibernate-orm","slug":"collection-element-mappingcontext-has-wrong-number","errorCode":null,"errorMessage":"collection element mappingContext has wrong number of columns: {} type: {}","messagePattern":"collection element mappingContext has wrong number of columns: (.+?) type: (.+?)","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/mapping/Collection.java","lineNumber":407,"sourceCode":"\t@Override\n\tpublic void setFetchStyle(FetchStyle fetchStyle) {\n\t\tthis.fetchStyle = fetchStyle;\n\t}\n\n\tpublic void validate(MappingContext mappingContext) throws MappingException {\n\t\tassert getKey() != null : \"Collection key not bound : \" + getRole();\n\t\tassert getElement() != null : \"Collection element not bound : \" + getRole();\n\n\t\tif ( !getKey().isValid( mappingContext ) ) {\n\t\t\tthrow new MappingException(\n\t\t\t\t\t\"collection foreign key mappingContext has wrong number of columns: \"\n\t\t\t\t\t\t\t+ getRole()\n\t\t\t\t\t\t\t+ \" type: \"\n\t\t\t\t\t\t\t+ getKey().getType().getName()\n\t\t\t);\n\t\t}\n\t\tif ( !getElement().isValid( mappingContext ) ) {\n\t\t\tthrow new MappingException(\n\t\t\t\t\t\"collection element mappingContext has wrong number of columns: \"\n\t\t\t\t\t\t\t+ getRole()\n\t\t\t\t\t\t\t+ \" type: \"\n\t\t\t\t\t\t\t+ getElement().getType().getName()\n\t\t\t);\n\t\t}\n\n\t\tcheckColumnDuplication();\n\t}\n\n\tprivate void checkColumnDuplication() throws MappingException {\n\t\tfinal String owner = \"collection '\" + getReferencedPropertyName() + \"'\";\n\t\tfinal HashSet<QualifiedColumnName> cols = new HashSet<>();\n\t\tgetKey().checkColumnDuplication( cols, owner );\n\t\tif ( isIndexed() ) {\n\t\t\t( (IndexedCollection) this ).getIndex().checkColumnDuplication( cols, owner );\n\t\t}\n\t\tif ( isIdentified() ) {","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/mapping/Collection.java#L389-L425","documentation":"Companion check to the key validation: the collection element value (element-collection column, target entity FK, or composite element) must have a type whose column span matches the mapped columns (element.isValid). A mismatch throws MappingException with the collection role and element type name.","triggerScenarios":"@ElementCollection over a custom type spanning two columns while only one @Column is declared; hbm <composite-element> with a wrong column count; many-to-many join columns not matching the target id span; formulas offsetting element column spans.","commonSituations":"Adding fields to embeddables without updating collection column lists; hand-maintained hbm components; switching attributes between basic and multi-column custom types.","solutions":["Make the element's declared columns match the element type's column span, in order.","For multi-column custom types, list all columns with @Columns({@Column(...), @Column(...)}).","Verify many-to-many join column lists against both endpoints' identifiers."],"exampleFix":"// before: TagUserType spans 2 columns but only one column declared\n@ElementCollection\n@Type(TagUserType.class)\n@Column(name = \"tag\")\nprivate List<Tag> tags;\n\n// after\n@ElementCollection\n@Type(TagUserType.class)\n@Columns({ @Column(name = \"tag\"), @Column(name = \"tag_lang\") })\nprivate List<Tag> tags;","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    SessionFactory sf = configuration.buildSessionFactory();\n}\ncatch ( MappingException e ) {\n    // message: collection element ... wrong number of columns: <role>\n    // compare declared element columns against the element type column span\n}","preventionTips":["List all columns of multi-column custom types with @Columns","Regenerate or review collection column lists after embeddable changes","Verify many-to-many join tables against both endpoint identifiers"],"tags":["hibernate","element-collection","mapping"],"backgroundTag":"column-count-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}