hibernate/hibernate-orm · error · AnnotationException

Association ' ' is 'mappedBy' a property named ' ' which…

Error message

Association '{}' is 'mappedBy' a property named '{}' which references the wrong entity type '{}', expected '{}'

What it means

Type-consistency check (checkMappedByType) for inverse associations: the property named by mappedBy on the target has a value whose referenced entity type does not match the owning entity (unless owner and target map the same table). The two sides of the bidirectional association point at the wrong types.

Solutions

  1. Correct the mappedBy value to reference a property of the expected entity type.
  2. Or fix the association target type so the mappedBy property belongs to it.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: An association declares 'mappedBy' naming a property that does not exist, has the wrong type, or is not the owning side on the target entity.

Common situations: Bidirectional associations where mappedBy is misspelled, both sides claim ownership, or the target entity was refactored without updating the mapping.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java:977

	private static void checkMappedByType(
			String mappedBy,
			String propertyName,
			PropertyHolder propertyHolder,
			Map<String, PersistentClass> persistentClasses,
			ToOne toOne) {
		final String referencedEntityName = toOne.getReferencedEntityName();
		final PersistentClass referencedClass = persistentClasses.get( referencedEntityName );
		PersistentClass ownerClass = propertyHolder.getPersistentClass();
		while ( ownerClass != null ) {
			if ( checkReferencedClass( ownerClass, referencedClass ) ) {
				// the two entities map to the same table, so we are good
				return;
			}
			ownerClass = ownerClass.getSuperPersistentClass();
		}
		// we could not find any entity mapping to the same table
		throw new AnnotationException(
				"Association '" + qualify( propertyHolder.getPath(), propertyName )
				+ "' is 'mappedBy' a property named '" + mappedBy
				+ "' which references the wrong entity type '" + referencedEntityName
				+ "', expected '" + propertyHolder.getEntityName() + "'"
		);
	}

	private static boolean checkReferencedClass(PersistentClass ownerClass, PersistentClass referencedClass) {
		while ( referencedClass != null ) {
			// Allow different entity types as long as they map to the same table
			if ( ownerClass.getTable() == referencedClass.getTable() ) {
				return true;
			}
			referencedClass = referencedClass.getSuperPersistentClass();
		}
		return false;
	}

View on GitHub (pinned to fad1729dce)