{"record":{"id":"d954d11bbf99fc91","repo":"hibernate/hibernate-orm","slug":"mismatch-between-number-of-collected-generated-col","errorCode":null,"errorMessage":"Mismatch between number of collected generated column values and number of columns for composite attribute: {}.{}","messagePattern":"Mismatch between number of collected generated column values and number of columns for composite attribute: (.+?)\\.(.+?)","errorType":"exception","errorClass":"CompositeValueGenerationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/generator/internal/CompositeGeneratorBuilder.java","lineNumber":140,"sourceCode":"\t\t\t\t\tif ( details != null ) {\n\t\t\t\t\t\tif ( generatorEventTypes.contains( eventType ) ) {\n\t\t\t\t\t\t\tif ( !onExecutionGenerator.referenceColumnsInSql( dialect, eventType ) ) {\n\t\t\t\t\t\t\t\tdetails.excludeColumns( columnIndex, span );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if ( onExecutionGenerator.writePropertyValue( eventType ) ) {\n\t\t\t\t\t\t\t\t// leave the default parameter marker values in place\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tfinal String[] referencedColumnValues =\n\t\t\t\t\t\t\t\t\t\tonExecutionGenerator.getReferencedColumnValues( dialect, eventType );\n\t\t\t\t\t\t\t\tif ( referencedColumnValues == null ) {\n\t\t\t\t\t\t\t\t\tthrow new CompositeValueGenerationException(\n\t\t\t\t\t\t\t\t\t\t\t\"Generated column values were not provided for composite attribute: \"\n\t\t\t\t\t\t\t\t\t\t\t+ mappingProperty.getName() + '.' + property.getName()\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif ( referencedColumnValues.length != span ) {\n\t\t\t\t\t\t\t\t\tthrow new CompositeValueGenerationException(\n\t\t\t\t\t\t\t\t\t\t\t\"Mismatch between number of collected generated column values and number of columns for composite attribute: \"\n\t\t\t\t\t\t\t\t\t\t\t+ mappingProperty.getName() + '.' + property.getName()\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tdetails.setColumnValues( columnIndex, referencedColumnValues );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if ( !onExecutionGenerator.allowMutation() ) {\n\t\t\t\t\t\t\tdetails.excludeColumns( columnIndex, span );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcolumnIndex += span;\n\t\t}\n\n\t\tfor ( var details : columnValueDetailsByEvent.values() ) {\n\t\t\tdetails.finalizeDetails();","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/generator/internal/CompositeGeneratorBuilder.java#L122-L158","documentation":"Thrown while Hibernate builds the on-execution value-generation plan for a composite (embeddable) attribute. When a sub-property's generator references its generated values in the SQL (referenceColumnsInSql()=true and the values are not written as parameters), the generator must supply exactly one SQL fragment per column of that sub-property via getReferencedColumnValues(dialect, eventType). This CompositeValueGenerationException reports that the returned array length differs from the sub-property's column span.","triggerScenarios":"Building a SessionFactory where an @Embeddable (or embedded id) member has an on-execution generator — e.g. @GeneratedColumn, identity columns, or a custom OnExecutionGenerator — whose getReferencedColumnValues(dialect, eventType) returns a String[] whose length != that member's column span. Example: a member mapped over 2 columns (@Columns or a CompositeUserType) whose generator returns one value, or a custom generator returning a hard-coded array.","commonSituations":"Applying @GeneratedColumn or trigger-based on-execution generation to multi-column members of embeddables; custom OnExecutionGenerator implementations that hard-code referenced column values; switching a member from a single column to a multi-column type (duration/range/period CompositeUserTypes) without updating the generator; Hibernate upgrades that tightened the composite generation contract.","solutions":["If you wrote the OnExecutionGenerator, make getReferencedColumnValues(dialect, eventType) return exactly one entry per column occupied by the sub-property (derive the count from the property's column span, not a constant)","Restrict on-execution generation to single-column members of the embeddable; move the generated value to a basic attribute on the entity","For multi-column members, switch to in-memory generation (@Generated(event=...) or a BeforeExecutionGenerator) that writes all columns as parameters","If only built-in annotations are involved, double-check @Columns / composite UserType mappings on the offending member (the message names Entity.embeddable.member)","If the mapping looks valid, report a Hibernate bug with the entity, embeddable, and generator class"],"exampleFix":"// before — member spans 2 columns but the generator supplies 1 referenced value\n@Embeddable\npublic class Availability {\n    @GeneratedColumn(event = EventType.INSERT)          // on-execution generation\n    @Columns({@Column(name = \"opens_at\"), @Column(name = \"closes_at\")})\n    MyLocalTimeRange window;                            // CompositeUserType over 2 columns\n}\n\n// after — on-execution generation only on a single-column member\n@Embeddable\npublic class Availability {\n    @GeneratedColumn(event = EventType.INSERT)\n    @Column(name = \"opens_at\")\n    Instant opensAt;\n\n    @Column(name = \"closes_at\")\n    Instant closesAt;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    sessionFactory = new MetadataSources(serviceRegistry)\n            .addAnnotatedClass(Order.class)\n            .buildMetadata()\n            .buildSessionFactory();\n}\ncatch (CompositeValueGenerationException e) {\n    // message names the offending path: \"<entity>.<embeddable>.<member>\"\n    throw new IllegalStateException(\"Invalid generated-value mapping: \" + e.getMessage(), e);\n}","preventionTips":["Keep @GeneratedColumn and on-execution generators on single-column basic attributes, not multi-column members of embeddables","Size getReferencedColumnValues(...) from the mapped column span, never a hard-coded array","Boot the SessionFactory in a CI smoke test so mapping errors fail the build instead of production"],"tags":["hibernate","value-generation","embeddable","composite-attribute","mapping","session-factory-bootstrap"],"backgroundTag":"generated-value-mapping-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}