{"record":{"id":"e69438bc5efe7c90","repo":"hibernate/hibernate-orm","slug":"an-association-that-targets-entity-referenced-e69438","errorCode":null,"errorMessage":"An association that targets entity '\" + referencedEntity.getEntityName() + \"' from entity '\" + associatedClass.getEntityName() + \"' has no '@JoinColumn' referencing column '\" + key.getColumns().get(j).getName() + \"'","messagePattern":"An association that targets entity '\" \\+ referencedEntity\\.getEntityName\\(\\) \\+ \"' from entity '\" \\+ associatedClass\\.getEntityName\\(\\) \\+ \"' has no '@JoinColumn' referencing column '\" \\+ key\\.getColumns\\(\\)\\.get\\(j\\)\\.getName\\(\\) \\+ \"'","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java","lineNumber":669,"sourceCode":"\tprivate static void bindPrimaryKeyReference(\n\t\t\tPersistentClass referencedEntity,\n\t\t\tAnnotatedJoinColumns joinColumns,\n\t\t\tSimpleValue value,\n\t\t\tPersistentClass associatedClass,\n\t\t\tMetadataBuildingContext buildingContext) {\n\t\t// ensure the composite key is sorted so that we can simply\n\t\t// set sorted to true on the ToOne (below)\n\t\tfinal var key = referencedEntity.getKey();\n\t\tif ( key instanceof Component component ) {\n\t\t\tcomponent.sortProperties();\n\t\t}\n\t\t// works because the pk has to be on the primary table\n\t\tfinal var metadataCollector = buildingContext.getMetadataCollector();\n\t\tfinal var dialect = metadataCollector.getDatabase().getJdbcEnvironment().getDialect();\n\t\tfor ( int j = 0; j < key.getColumnSpan(); j++ ) {\n\t\t\tif ( !matchUpJoinColumnsWithKeyColumns( referencedEntity, joinColumns, value, metadataCollector, dialect, j ) ) {\n\t\t\t\t// we can only get here if there's a dupe PK column in the @JoinColumns\n\t\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\t\"An association that targets entity '\" + referencedEntity.getEntityName()\n\t\t\t\t\t\t\t\t+ \"' from entity '\" + associatedClass.getEntityName()\n\t\t\t\t\t\t\t\t+ \"' has no '@JoinColumn' referencing column '\" + key.getColumns().get(j).getName() + \"'\"\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tif ( value instanceof ToOne toOne ) {\n\t\t\ttoOne.setSorted( true );\n\t\t}\n\t\telse if ( value instanceof DependantValue dependantValue ) {\n\t\t\tdependantValue.setSorted( true );\n\t\t}\n\t\telse {\n\t\t\tthrow new AssertionError(\n\t\t\t\t\t\"This should never happen, value can only be ToOne or DependantValue,\" +\n\t\t\t\t\t\t\t\"instead it's '\" + value.getClass().getName() + \"'\"\n\t\t\t);\n\t\t}","sourceCodeStart":651,"sourceCodeEnd":687,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java#L651-L687","documentation":"Error \"An association that targets entity '\" + referencedEntity.getEntityName() + \"' from entity '\" + associatedClass.getEntityName() + \"' has no '@JoinColumn' referencing column '\" + key.getColumns().get(j).getName() + \"'\" 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 a @JoinColumn referencing the missing primary key column 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"}