hibernate/hibernate-orm · error · IllegalArgumentException

Argument ' ' could not be converted to the identifier type…

Error message

Argument '%s' could not be converted to the identifier type of entity '%s': %s

What it means

Thrown by Helper.coerceId when attempting to coerce the given id argument to the entity's identifier type raised a CoercionException — the value cannot be converted (e.g. 'abc' to Long). The original failure is chained.

Solutions

  1. Convert the argument to the entity identifier type (e.g. Long.valueOf) before calling, or fix the entity's id type mapping.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/internal/find/Helper.java:51 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/find/Helper.java:51

		final var identifierMapping = entityPersister.getIdentifierMapping();
		if ( isLoadByIdComplianceEnabled( factory ) ) {
			final var javaType = identifierMapping.getJavaType();
			if ( !identifierMapping.isVirtual() && !javaType.isInstance( id ) ) {
				// per expectation of EntityHandler#find / EntityHandler#get
				throw new IllegalArgumentException(
						"Given value '%s' did not match expected identifier type '%s' of entity '%s'"
								.formatted( id, javaType.getTypeName(), entityPersister.getEntityName() ) );
			}
			return id;
		}
		else {
			try {
				return identifierMapping.isVirtual()
						? id // special case for a class with an @IdClass
						: identifierMapping.getJavaType().coerce( id );
			}
			catch ( Exception e ) {
				throw new IllegalArgumentException(
						"Argument '%s' could not be converted to the identifier type of entity '%s': %s"
								.formatted( id, entityPersister.getEntityName(), e.getMessage() ),
						e
				);
			}
		}
	}

	public static Object[] coerceIds(EntityPersister entityPersister, List<?> keys, SharedSessionContractImplementor session) {
		Object[] ids = null;
		final int size = keys.size();
		for ( int i = 0; i < size; i++ ) {
			final var factory = session.getFactory();
			final Object key = keys.get( i );
			final Object coerced = coerceId( entityPersister, key, factory );
			if ( coerced != key ) {
				if ( ids == null ) {
					ids = new Object[size];

View on GitHub (pinned to fad1729dce)