hibernate/hibernate-orm · error · TypeMismatchException

Supplied id had wrong type: entity '${persister.getEntityNam

Error message

Supplied id had wrong type: entity '${persister.getEntityName()}' has id type '${idClass}' but supplied id was of type '${event.getEntityId().getClass()}'

What it means

Error "Supplied id had wrong type: entity '${persister.getEntityName()}' has id type '${idClass}' but supplied id was of type '${event.getEntityId().getClass()}'" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java:68

	 * @param event The load event to be handled.
	 */
	@Override
	public void onLoad(@Nonnull LoadEvent event, @Nonnull LoadType loadType) {
		final var persister = getPersister( event );
		if ( persister == null ) {
			throw new HibernateException( "Unable to locate persister: " + event.getEntityClassName() );
		}
		checkId( event, loadType, persister );
		doOnLoad( persister, event, loadType );
	}

	private void checkId(@Nonnull LoadEvent event, @Nonnull LoadType loadType, @Nonnull EntityPersister persister) {
		final Object id = event.getEntityId();
		if ( !persister.getIdentifierMapping().getJavaType().isInstance( id )
				&& !( id instanceof DelayedPostInsertIdentifier ) ) {
			final Class<?> idClass = persister.getIdentifierType().getReturnedClass();
			if ( handleIdType( persister, event, loadType, idClass ) ) {
				throw new TypeMismatchException(
						"Supplied id had wrong type: entity '" + persister.getEntityName()
								+ "' has id type '" + idClass
								+ "' but supplied id was of type '" + event.getEntityId().getClass() + "'"
				);
			}
		}
	}

	@Nullable
	protected EntityPersister getPersister(@Nonnull final LoadEvent event) {
		final Object instanceToLoad = event.getInstanceToLoad();
		if ( instanceToLoad != null ) {
			//the load() which takes an entity does not pass an entityName
			event.setEntityClassName( instanceToLoad.getClass().getName() );
			return event.getSession().getEntityPersister( null, instanceToLoad );
		}
		else {
			return event.getFactory().getMappingMetamodel().getEntityDescriptor( event.getEntityClassName() );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Pass an identifier whose Java type matches the entity's @Id type (e.g. Long vs Integer).
  2. Check for auto-boxing or deserialization that produced the wrong id wrapper type.

When it happens

Trigger: Thrown when a supplied argument's Java type does not match the type expected by the mapping or parameter definition.

Common situations: Typical situations: passing an id of the wrong Java type; binding a filter/query parameter with an incompatible value; generic type erasure hiding a wrong argument type.


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