{"record":{"id":"21c8b9d66610f011","repo":"hibernate/hibernate-orm","slug":"association-path-targets-the-type-type-w","errorCode":null,"errorMessage":"Association '${path}' targets the type '${type}' which does not belong to the same persistence unit","messagePattern":"Association '(.+?)' targets the type '(.+?)' which does not belong to the same persistence unit","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java","lineNumber":91,"sourceCode":"\t\tif ( mappedBy == null ) {\n\t\t\tbindOwned( persistentClasses, oneToOne, inferredData.getPropertyName() );\n\t\t}\n\t\telse {\n\t\t\tbindUnowned( persistentClasses, oneToOne );\n\t\t}\n\t\tbinder.callAttributeBindersInSecondPass( property );\n\t\toneToOne.sortProperties();\n\t}\n\n\tprivate void bindUnowned(Map<String, PersistentClass> persistentClasses, OneToOne oneToOne) {\n\t\toneToOne.setMappedByProperty( mappedBy );\n\t\tfinal String targetEntityName = oneToOne.getReferencedEntityName();\n\t\tfinal var targetEntity = persistentClasses.get( targetEntityName );\n\t\tif ( targetEntity == null ) {\n\t\t\tfinal String problem = annotatedEntity\n\t\t\t\t\t? \" which does not belong to the same persistence unit\"\n\t\t\t\t\t: \" which is not an '@Entity' type\";\n\t\t\tthrow new MappingException( \"Association '\" + getPath( propertyHolder, inferredData )\n\t\t\t\t\t+ \"' targets the type '\" + targetEntityName + \"'\" + problem );\n\t\t}\n\t\tfinal var targetProperty = targetProperty( oneToOne, targetEntity );\n\t\tfinal var targetPropertyValue = targetProperty.getValue();\n\t\tif ( targetPropertyValue instanceof ManyToOne ) {\n\t\t\tbindTargetManyToOne( persistentClasses, oneToOne, targetEntity, targetProperty );\n\t\t}\n\t\telse if ( !(targetPropertyValue instanceof OneToOne) ) {\n\t\t\tthrow new AnnotationException( \"Association '\" + getPath( propertyHolder, inferredData )\n\t\t\t\t\t+ \"' is 'mappedBy' a property named '\" + mappedBy\n\t\t\t\t\t+ \"' of the target entity type '\" + targetEntityName\n\t\t\t\t\t+ \"' which is not a '@OneToOne' or '@ManyToOne' association\" );\n\t\t}\n\t\tcheckMappedByType(\n\t\t\t\tmappedBy,\n\t\t\t\ttargetPropertyValue,\n\t\t\t\toneToOne.getPropertyName(),\n\t\t\t\tpropertyHolder,","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java#L73-L109","documentation":"During the second pass of @OneToOne binding, Hibernate looks the association's target entity name up in the mapped-class registry of the persistence unit. The lookup returned null for a class that is annotated as an entity, so it concludes the type was never included in this persistence unit and fails with MappingException at bootstrap.","triggerScenarios":"@OneToOne(targetEntity = Other.class) or @OneToOne on a field of type Other, where Other is a @Entity class that is not part of the metadata being built: not listed in persistence.xml <class>, outside the @EntityScan base packages in Spring Boot, missing from hibernate.cfg.xml <mapping class=...>, or discovered by a different EntityManagerFactory.","commonSituations":"Spring Boot apps where the associated entity lives in a module/package outside the default scan root; classic persistence.xml setups that require explicit <class> entries and missed one; multiple persistence units where the entity is registered in unit A but referenced from unit B; moving entities between modules during refactoring; test bootstrap that maps a subset of classes.","solutions":["Add the target class to the persistence unit: persistence.xml <class>com.example.Other</class> or hibernate.cfg.xml <mapping class=\"com.example.Other\"/>.","In Spring Boot, extend @EntityScan basePackages (or the auto-configuration's scan) so the package containing the target entity is included.","If multiple persistence units exist, make sure both sides of the association are in the same unit.","If the target was never meant to be an entity, remove targetEntity / change the field type, since a @OneToOne must target a managed entity."],"exampleFix":"// before: Other is @Entity but not in the persistence unit\n@OneToOne\nprivate Other other;\n\n// persistence.xml\n<persistence-unit name=\"pu\">\n  <class>com.example.MainEntity</class>\n  <!-- Other missing -->\n</persistence-unit>\n\n// after\n<persistence-unit name=\"pu\">\n  <class>com.example.MainEntity</class>\n  <class>com.example.Other</class>\n</persistence-unit>","handlingStrategy":"try-catch","validationCode":"// Verify every association target resolves to a class in the mapped set\nSet<Class<?>> mapped = Set.copyOf(annotatedClasses);\nfor (Class<?> entity : mapped) {\n    for (Field f : entity.getDeclaredFields()) {\n        Class<?> target = f.getType();\n        OneToOne o2o = f.getAnnotation(OneToOne.class);\n        if (o2o != null && o2o.targetEntity() != void.class) target = o2o.targetEntity();\n        if (o2o != null && !target.isAnnotationPresent(Entity.class)) {\n            throw new IllegalStateException(f + \" targets non-entity \" + target);\n        }\n    }\n}","typeGuard":"static boolean isMappedEntity(Class<?> candidate, Set<Class<?>> mappedUnits) {\n    return candidate.isAnnotationPresent(Entity.class) && mappedUnits.contains(candidate);\n}","tryCatchPattern":"try {\n    Metadata md = new MetadataSources(registry).addAnnotatedClasses(annotated).buildMetadata();\n} catch (MappingException e) {\n    // 'does not belong to the same persistence unit' => fix classlist; surface a class-list diff\n    throw new IllegalStateException(\"Association target missing from persistence unit: \" + e.getMessage(), e);\n}","preventionTips":["Keep @EntityScan basePackages broad enough to cover association targets in modular projects","List every entity in persistence.xml when not using scanning","Run a CI test that boots the full persistence unit"],"tags":["hibernate","jpa","one-to-one","association","persistence-unit","entity-scan","bootstrap"],"backgroundTag":"unmapped-entity-target","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}