hibernate/hibernate-orm · error · AnnotationException

Association '${role}' targets the type '${type}' which does

Error message

Association '${role}' targets the type '${type}' which does not belong to the same persistence unit

What it means

Error "Association '${role}' targets the type '${type}' which does not belong to the same persistence unit" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/internal/MapBinder.java:269

						.getClassDetailsRegistry()
						.resolveClassDetails( mapKeyType );
	}

	private static String getKeyType(MemberDetails property) {
		//target has priority over reflection for the map key type
		//JPA 2 has priority
		final var mapKeyClassAnn = property.getDirectAnnotationUsage( MapKeyClass.class );
		final var target = mapKeyClassAnn != null ? mapKeyClassAnn.value() : void.class;
		return void.class.equals( target ) ? property.getMapKeyType().getName() : target.getName();
	}

	private void handleMapKeyProperty(
			TypeDetails elementType,
			java.util.Map<String, PersistentClass> persistentClasses,
			String mapKeyPropertyName) {
		final var associatedClass = persistentClasses.get( elementType.getName() );
		if ( associatedClass == null ) {
			throw new AnnotationException( "Association '" + safeCollectionRole() + "'"
					+ targetEntityMessage( elementType ) );
		}
		final Property mapProperty = findPropertyByName( associatedClass, mapKeyPropertyName );
		if ( mapProperty == null ) {
			throw new AnnotationException( "Map key property '" + mapKeyPropertyName
					+ "' not found in target entity '" + associatedClass.getEntityName() + "'" );
		}
		// HHH-11005 - if InheritanceType.JOINED then need to find class defining the column
		final var targetEntity =
				inheritanceStatePerClass.get( elementType.determineRawClass() ).getType() == InheritanceType.JOINED
						? mapProperty.getPersistentClass()
						: associatedClass;
		final Value indexValue =
				createFormulatedValue( mapProperty.getValue(), collection, associatedClass, targetEntity );
		getMap().setIndex( indexValue );
		getMap().setMapKeyPropertyName( mapKeyPropertyName );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Include the target type in the same persistence unit, or change the association target.

When it happens

Trigger: An association or reference targets a type that Hibernate has not mapped as an entity.

Common situations: Missing @Entity on the target class, the class not being scanned into the persistence unit, or a typo in the entity name.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/879294911ba58d94. Report an issue: GitHub.