hibernate/hibernate-orm · error · MappingException

Plural attribute [ . ] was mapped with targetEntity=` `…

Error message

Plural attribute [%s.%s] was mapped with targetEntity=`%s`, but the attribute is declared as `%s`

What it means

Mapping validation in checkElementType: @OneToMany/@ManyToMany declared targetEntity=X but the attribute's declared Java element type is incompatible with X (not a supertype match), so the collection cannot be typed consistently.

Solutions

  1. Remove targetEntity from the mapping so it matches the declared attribute type, or change targetEntity to the declared element type.
  2. Fix the generic type of the collection attribute to match the intended target entity.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown when @OneToMany(targetEntity=...) names a class that is not assignable to the declared generic element type of the collection.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java:247

			ManagedMappingType declaringType,
			MappingModelCreationProcess creationProcess) {
		return creationProcess.getCreationContext().getBootstrapContext()
				.getModelsContext().getClassDetailsRegistry()
				.resolveClassDetails( declaringType.getJavaType().getTypeName() );
	}

	private static void checkElementType(
			EntityCollectionPart elementPart,
			ManagedMappingType declaringType,
			String attributeName,
			MemberDetails attributeMemberDetails) {
		final var elementType =
				attributeMemberDetails.getElementType()
						.determineRawClass().toJavaClass();
		if ( !Object.class.equals( elementType ) ) {
			final var targetType = elementPart.getJavaType().getJavaTypeClass();
			if ( !elementType.isAssignableFrom( targetType ) ) {
				throw new MappingException(
						String.format(
								ROOT,
								"Plural attribute [%s.%s] was mapped with targetEntity=`%s`,"
										+ " but the attribute is declared as `%s`",
								declaringType.getNavigableRole().getFullPath(),
								attributeName,
								targetType.getName(),
								elementType.getName()
						)
				);
			}
		}
	}

	private static @Nullable MemberDetails getMemberDetails(
			String attributeName, PropertyAccess propertyAccess, ClassDetails declaringClassDetails) {
		final var member = propertyAccess.getGetter().getMember();
		if ( member instanceof Field ) {

View on GitHub (pinned to fad1729dce)