hibernate/hibernate-orm · error · IdentifierGenerationException

Entity '{}' has a '@NaturalId' property which is also define

Error message

Entity '{}' has a '@NaturalId' property which is also defined as insert-generated

What it means

Error "Entity '{}' has a '@NaturalId' property which is also defined as insert-generated" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/NaturalIdHelper.java:28

import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.persister.entity.EntityPersister;

import static org.hibernate.engine.internal.NaturalIdLogging.NATURAL_ID_LOGGER;
import static org.hibernate.pretty.MessageHelper.infoString;

/**
 * @author Gavin King
 */
public class NaturalIdHelper {

	public static String[] getNaturalIdPropertyNames(EntityPersister persister) {
		final int[] naturalIdPropertyIndices = persister.getNaturalIdentifierProperties();
		if ( naturalIdPropertyIndices == null ) {
			throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
					+ "' has no '@NaturalId' property" );
		}
		if ( persister.isNaturalIdentifierInsertGenerated() ) {
			throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
					+ "' has a '@NaturalId' property which is also defined as insert-generated" );
		}
		final String[] allPropertyNames = persister.getPropertyNames();
		final String[] propertyNames = new String[naturalIdPropertyIndices.length];
		for ( int i = 0; i < naturalIdPropertyIndices.length; i++ ) {
			propertyNames[i] = allPropertyNames[naturalIdPropertyIndices[i]];
		}
		return propertyNames;
	}

	public static void performAnyNeededCrossReferenceSynchronizations(
			boolean synchronizationEnabled,
			EntityMappingType entityMappingType,
			SharedSessionContractImplementor session) {
		// first check if synchronization (this process) was disabled
		if ( synchronizationEnabled
				// only mutable natural-ids need this processing
				&& entityMappingType.requireNaturalIdMapping().isMutable()

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove @Generated(insert-generated) from the @NaturalId property, or use a different property as the natural id.

When it happens

Trigger: Thrown when a natural-id operation is requested on an entity without a valid @NaturalId mapping.

Common situations: Typical situations: calling byNaturalId() for an entity lacking @NaturalId; a natural id also mapped as insert-generated.


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