hibernate/hibernate-orm · error · AnnotationException

Use of the same entity name twice

Error message

Use of the same entity name twice: ${name}

What it means

registerImportName failure: metadataCollector.addImport threw a MappingException because the entity name (or its short name) is already registered by a different entity. Two entities are mapped with the same name in this persistence unit.

Solutions

  1. Rename one of the entities so each entity name is unique in the persistence unit.
Defensive patterns

Strategy: validation

When it happens

Trigger: Two managed types are registered under the same entity name.

Common situations: Duplicate @Entity(name=...) values or two classes with the same simple name in one persistence unit.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java:1489

			throw new MappingException( "@Mutability is not allowed on entity" );
		}
	}

	private boolean isMutable() {
		return !annotatedClass.hasAnnotationUsage( Immutable.class, modelsContext() );
	}

	private void registerImportName() {
		try {
			final var metadataCollector = getMetadataCollector();
			metadataCollector.addImport( name, persistentClass.getEntityName() );
			final String entityName = persistentClass.getEntityName();
			if ( !entityName.equals( name ) ) {
				metadataCollector.addImport( entityName, entityName );
			}
		}
		catch (MappingException me) {
			throw new AnnotationException( "Use of the same entity name twice: " + name, me );
		}
	}

	private void bindRootEntity() {
		final var rootClass = (RootClass) persistentClass;
		rootClass.setMutable( isMutable() );
		if ( isNotBlank( where ) ) {
			rootClass.setWhere( where );
		}
		if ( cacheConcurrentStrategy != null ) {
			rootClass.setCacheConcurrencyStrategy( cacheConcurrentStrategy );
			rootClass.setCacheRegionName( cacheRegion );
			rootClass.setLazyPropertiesCacheable( cacheLazyProperty );
		}
		rootClass.setNaturalIdCacheRegionName( naturalIdCacheRegion );
		rootClass.setNaturalIdClass( naturalIdClass );
	}

View on GitHub (pinned to fad1729dce)