{"record":{"id":"96ad1385292c7582","repo":"hibernate/hibernate-orm","slug":"an-association-that-targets-entity-referenced","errorCode":null,"errorMessage":"An association that targets entity '\" + referencedEntity.getEntityName() + \"' from entity '\" + associatedClass.getEntityName() + \"' has \" + joinColumns.getColumns().size() + \" '@JoinColumn's but the primary key has \" + referencedEntity.getIdentifier().getColumnSpan() + \" columns\"","messagePattern":"An association that targets entity '\" \\+ referencedEntity\\.getEntityName\\(\\) \\+ \"' from entity '\" \\+ associatedClass\\.getEntityName\\(\\) \\+ \"' has \" \\+ joinColumns\\.getColumns\\(\\)\\.size\\(\\) \\+ \" '@JoinColumn's but the primary key has \" \\+ referencedEntity\\.getIdentifier\\(\\)\\.getColumnSpan\\(\\) \\+ \" columns\"","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java","lineNumber":633,"sourceCode":"\t\t\t\tbindNonPrimaryKeyReference( referencedEntity, joinColumns, value );\n\t\t\t\tbreak;\n\t\t\tcase IMPLICIT_PRIMARY_KEY_REFERENCE:\n\t\t\t\tbindImplicitPrimaryKeyReference( referencedEntity, joinColumns, value, associatedClass );\n\t\t\t\tbreak;\n\t\t\tcase EXPLICIT_PRIMARY_KEY_REFERENCE:\n\t\t\t\tbindPrimaryKeyReference( referencedEntity, joinColumns, value, associatedClass, buildingContext );\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\tprivate static void bindImplicitPrimaryKeyReference(\n\t\t\tPersistentClass referencedEntity,\n\t\t\tAnnotatedJoinColumns joinColumns,\n\t\t\tSimpleValue value,\n\t\t\tPersistentClass associatedClass) {\n\t\t//implicit case, we hope PK and FK columns are in the same order\n\t\tif ( joinColumns.getColumns().size() != referencedEntity.getIdentifier().getColumnSpan() ) {\n\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\"An association that targets entity '\" + referencedEntity.getEntityName()\n\t\t\t\t\t\t\t+ \"' from entity '\" + associatedClass.getEntityName()\n\t\t\t\t\t\t\t+ \"' has \" + joinColumns.getColumns().size() + \" '@JoinColumn's but the primary key has \"\n\t\t\t\t\t\t\t+ referencedEntity.getIdentifier().getColumnSpan() + \" columns\"\n\t\t\t);\n\t\t}\n\t\tlinkJoinColumnWithValueOverridingNameIfImplicit(\n\t\t\t\treferencedEntity,\n\t\t\t\treferencedEntity.getIdentifier(),\n\t\t\t\tjoinColumns,\n\t\t\t\tvalue\n\t\t);\n\t\tif ( value instanceof SortableValue sortableValue ) {\n\t\t\tsortableValue.sortProperties();\n\t\t}\n\t}\n\n\tprivate static void bindPrimaryKeyReference(","sourceCodeStart":615,"sourceCodeEnd":651,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java#L615-L651","documentation":"Error \"An association that targets entity '\" + referencedEntity.getEntityName() + \"' from entity '\" + associatedClass.getEntityName() + \"' has \" + joinColumns.getColumns().size() + \" '@JoinColumn's but the primary key has \" + referencedEntity.getIdentifier().getColumnSpan() + \" columns\"\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A '@JoinColumn' / 'referencedColumnName' does not line up with the columns mapped by the target entity.","commonSituations":"Composite foreign keys, associations to entities with secondary tables, or reordered/duplicated referencedColumnName values across multiple @JoinColumn declarations.","solutions":["Add or remove @JoinColumn entries so their count matches the primary key column count of the target entity."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}