{"record":{"id":"d741c347e66c7aae","repo":"hibernate/hibernate-orm","slug":"to-one-mapping-s-s-was-mapped-with-targetentit","errorCode":null,"errorMessage":"To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`","messagePattern":"To-one mapping \\[(.+?)\\.(.+?)\\] was mapped with targetEntity=`(.+?)`, but the attribute is declared as `(.+?)`","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java","lineNumber":272,"sourceCode":"\t\t\t\tattributeMetadata,\n\t\t\t\tadjustFetchTiming( mappedFetchTiming, bootValue ),\n\t\t\t\tmappedFetchStyle,\n\t\t\t\tdeclaringType,\n\t\t\t\tpropertyAccess\n\t\t);\n\n\n\t\ttemporalTableStrategy =\n\t\t\t\tdeclaringEntityPersister.getFactory()\n\t\t\t\t\t\t.getSessionFactoryOptions().getTemporalTableStrategy();\n\n\t\tif ( entityMappingType.getRepresentationStrategy().getMode() == RepresentationMode.POJO ) {\n\t\t\t// validate the type.\n\t\t\tvar declaredType = propertyAccess.getGetter().getReturnTypeClass();\n\t\t\tif ( !Object.class.equals( declaredType ) ) {\n\t\t\t\tfinal var targetType = entityMappingType.getMappedJavaType().getJavaTypeClass();\n\t\t\t\tif ( !declaredType.isAssignableFrom( targetType ) ) {\n\t\t\t\t\tthrow new MappingException(\n\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\t\t\"To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`\",\n\t\t\t\t\t\t\t\t\tdeclaringType.getNavigableRole().getFullPath(),\n\t\t\t\t\t\t\t\t\tname,\n\t\t\t\t\t\t\t\t\ttargetType.getName(),\n\t\t\t\t\t\t\t\t\tdeclaredType.getName()\n\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\n\t\tsqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name );\n\t\tisNullable = bootValue.isNullable();\n\t\tisLazy = navigableRole.getParent().getParent() == null\n\t\t\t\t\t&& declaringEntityPersister.getBytecodeEnhancementMetadata()\n\t\t\t\t\t\t\t.getLazyAttributesMetadata()","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java#L254-L290","documentation":"Error \"To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when @ManyToOne/@OneToOne(targetEntity=...) names a class not assignable to the declared attribute type.","commonSituations":"See trigger scenarios.","solutions":["Remove targetEntity from the to-one mapping, or set it equal to the attribute's declared type.","Fix the attribute's declared type to match the intended 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"}