hibernate/hibernate-orm · error · MappingException

To-one mapping [%s.%s] was mapped with targetEntity=`%s`, bu

Error message

To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`

What it means

Error "To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java:272

				attributeMetadata,
				adjustFetchTiming( mappedFetchTiming, bootValue ),
				mappedFetchStyle,
				declaringType,
				propertyAccess
		);


		temporalTableStrategy =
				declaringEntityPersister.getFactory()
						.getSessionFactoryOptions().getTemporalTableStrategy();

		if ( entityMappingType.getRepresentationStrategy().getMode() == RepresentationMode.POJO ) {
			// validate the type.
			var declaredType = propertyAccess.getGetter().getReturnTypeClass();
			if ( !Object.class.equals( declaredType ) ) {
				final var targetType = entityMappingType.getMappedJavaType().getJavaTypeClass();
				if ( !declaredType.isAssignableFrom( targetType ) ) {
					throw new MappingException(
							String.format(
									Locale.ROOT,
									"To-one mapping [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`",
									declaringType.getNavigableRole().getFullPath(),
									name,
									targetType.getName(),
									declaredType.getName()
							)
					);
				}
			}
		}

		sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName( name );
		isNullable = bootValue.isNullable();
		isLazy = navigableRole.getParent().getParent() == null
					&& declaringEntityPersister.getBytecodeEnhancementMetadata()
							.getLazyAttributesMetadata()

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove targetEntity from the to-one mapping, or set it equal to the attribute's declared type.
  2. Fix the attribute's declared type to match the intended target entity.

When it happens

Trigger: Thrown when @ManyToOne/@OneToOne(targetEntity=...) names a class not assignable to the declared attribute type.

Common situations: See trigger scenarios.


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