{"record":{"id":"d50741b811c5da54","repo":"hibernate/hibernate-orm","slug":"attribute-s-of-entity-s-is-mapped-by-associa-d50741","errorCode":null,"errorMessage":"Attribute '%s' of entity '%s' is mapped by association '%s' but is not annotated '@Id'","messagePattern":"Attribute '(.+?)' of entity '(.+?)' is mapped by association '(.+?)' but is not annotated '@Id'","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java","lineNumber":1540,"sourceCode":"\t\t\t\tfinal String expectedTypeName = compositeId.getComponentClassName();\n\t\t\t\tfinal String actualTypeName = property.getType().getName();\n\t\t\t\tif ( !hasCompatibleType( actualTypeName, expectedTypeName ) ) {\n\t\t\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\t\t\"Identifier attribute '%s' of entity '%s' has type '%s' but is mapped by association '%s' to entity '%s' with composite identifier type '%s'\"\n\t\t\t\t\t\t\t\t\t.formatted(\n\t\t\t\t\t\t\t\t\t\t\tpropertyName,\n\t\t\t\t\t\t\t\t\t\t\tpropertyHolder.getPersistentClass().getEntityName(),\n\t\t\t\t\t\t\t\t\t\t\tactualTypeName,\n\t\t\t\t\t\t\t\t\t\t\tmapsIdProperty.getPropertyName(),\n\t\t\t\t\t\t\t\t\t\t\treferencedEntityName,\n\t\t\t\t\t\t\t\t\t\t\texpectedTypeName\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif ( !isSimpleId( property ) ) {\n\t\t\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\t\t\"Attribute '%s' of entity '%s' is mapped by association '%s' but is not annotated '@Id'\"\n\t\t\t\t\t\t\t\t\t.formatted(\n\t\t\t\t\t\t\t\t\t\t\tpropertyName,\n\t\t\t\t\t\t\t\t\t\t\tpropertyHolder.getPersistentClass().getEntityName(),\n\t\t\t\t\t\t\t\t\t\t\tmapsIdProperty.getPropertyName()\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":1522,"sourceCodeEnd":1553,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java#L1522-L1553","documentation":"Thrown by the else-branch of checkMappedId: @MapsId points at an entity whose identifier is simple (not a Component), so the dependent attribute must be a simple @Id. If isSimpleId(property) fails — the attribute is @EmbeddedId, @Embedded, or lacks @Id entirely — bootstrap fails with this AnnotationException.","triggerScenarios":"Parent has @Id Long id; child declares @ManyToOne @MapsId Parent parent together with @EmbeddedId ChildId (wrong shape), or the referenced parent attribute has no @Id at all. Fires when the referenced entity binding exists and getIdentifier() is not a Component.","commonSituations":"Switching a parent from composite to simple key while the child keeps @EmbeddedId; applying a composite-key MapsId template to a simple-key parent; annotating the mapped attribute with @Embedded instead of @Id; removing @Id during refactoring.","solutions":["Replace the child's @EmbeddedId with a simple @Id attribute and keep @MapsId on the association (shared primary key pattern).","Or annotate the mapped attribute itself with @Id (JPA derived identity variant: @Id on the @ManyToOne).","If the child genuinely needs a composite key, give the parent a composite @EmbeddedId so both sides match shapes."],"exampleFix":"// before (parent has @Id Long id)\n@EmbeddedId\nprivate ChildId id; // wrong: parent key is simple\n@ManyToOne(fetch = FetchType.LAZY)\n@MapsId(\"parentId\")\nprivate Parent parent;\n\n// after\n@Id\nprivate Long id; // same type as parent's simple id\n\n@ManyToOne(fetch = FetchType.LAZY)\n@MapsId\nprivate Parent parent;","handlingStrategy":"validation","validationCode":"// assert simple-id parents are paired with simple @Id attributes\nif (hasSimpleId(Parent.class) && idAttribute(Child.class).isAnnotationPresent(EmbeddedId.class)) {\n    throw new IllegalStateException(\"Child of simple-id parent must use @Id, not @EmbeddedId\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    metadata = sources.buildMetadata();\n} catch (AnnotationException e) {\n    failBuild(\"MapsId shape mismatch: \" + e.getMessage());\n}","preventionTips":["When simplifying a parent key to @Id, update all @MapsId children in the same commit.","Keep a checklist of id-shape pairs (simple->simple, composite->composite) in the mapping module docs."],"tags":["hibernate","orm","mapsid","id-mapping","mapping"],"backgroundTag":"jpa-mapsid-misuse","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}