hibernate/hibernate-orm · error · IllegalStateException

Unable to locate id type for type [{}]

Error message

Unable to locate id type for type [{}]

What it means

getIdType() must return the simple domain type of the entity's identifier. It tries findIdAttribute(), then the @IdClass route (single id-class attribute, or the idClassType if it is a SimpleDomainType). If none apply — no id attribute anywhere in the hierarchy and no usable @IdClass type — Hibernate throws IllegalStateException, which almost always means the metadata is inconsistent or the type was built before its id mapping was wired up.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java:202

	@Override
	@Nonnull
	public SimpleDomainType<?> getIdType() {
		final var id = findIdAttribute();
		if ( id != null ) {
			return id.getType();
		}
		else {
			final var idClassAttributes = getIdClassAttributesSafely();
			if ( idClassAttributes != null ) {
				if ( idClassAttributes.size() == 1 ) {
					return idClassAttributes.iterator().next().getType();
				}
				else if ( idClassType instanceof SimpleDomainType<?> simpleDomainType ) {
					return simpleDomainType;
				}
			}
			throw new IllegalStateException( "Unable to locate id type for type [" + getTypeName() + "]" );
		}
	}

	/**
	 * A form of {@link #getIdClassAttributes} which prefers to return {@code null} rather than throw exceptions
	 *
	 * @return IdClass attributes or {@code null}
	 */
	public Set<SingularPersistentAttribute<? super J, ?>> getIdClassAttributesSafely() {
		if ( hasIdClass() ) {
			final Set<SingularPersistentAttribute<? super J, ?>> attributes = new HashSet<>();
			visitIdClassAttributes( attributes::add );
			return attributes.isEmpty() ? null : attributes;
		}
		else {
			return null;
		}
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Verify the entity actually declares an id (@Id/@EmbeddedId/@IdClass) and that mapping is complete before querying the metamodel
  2. Delay metamodel access until after SessionFactory/EntityManagerFactory bootstrap completes
  3. If it reproduces on a stable mapping, check for a Hibernate version bug — update to the latest 6.x/7.x patch

Example fix

// before
SimpleDomainType<?> idType = entityType.getIdType(); // during bootstrap -> may throw

// after
// access after factory build:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
SimpleDomainType<?> idType = emf.getMetamodel().entity(Order.class).getIdType();
Defensive patterns

Strategy: validation

Validate before calling

// Only ask for id type on fully mapped identifiable types after bootstrap
if (identifiableType.hasIdClass()) {
    // ok: id type comes from the id class
} else if (identifiableType.getId(...) == null) {
    throw new IllegalStateException("entity missing id mapping: " + identifiableType.getTypeName());
}
SimpleDomainType<?> idType = identifiableType.getIdType();

Try / catch

try {
    return type.getIdType();
} catch (IllegalStateException e) {
    if (e.getMessage() != null && e.getMessage().contains("Unable to locate id type")) {
        // re-verify mapping; entity may lack an id or bootstrap is incomplete
        throw new IllegalStateException("Mapping defect on " + type.getTypeName(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling getIdType() during bootstrap before the id attribute was registered (metamodel built incrementally); on an embeddable or mapped-superclass incorrectly cast to an identifiable type; an @IdClass whose type is not resolvable as a SimpleDomainType with more than one attribute.

Common situations: Metamodel objects requested from the SessionFactory while mapping metadata is still being initialized (cyclic id references). Custom or programmatic mappings that forgot to declare an id. Bugs in older Hibernate 6.x versions during entity enhancement or lazy metadata init — upgrade if applicable.

Related errors


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