hibernate/hibernate-orm · error · PersistenceException

Hibernate cannot unwrap '{getClass().getName()}' as '{type.g

Error message

Hibernate cannot unwrap '{getClass().getName()}' as '{type.getName()}'

What it means

Error "Hibernate cannot unwrap '{getClass().getName()}' as '{type.getName()}'" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java:2783

		catch ( RuntimeException e ) {
			throw getExceptionConverter().convert( e );
		}
	}

	@Override
	@Nonnull
	public <T> T unwrap(@Nonnull Class<T> type) {
		checkOpen();

		if ( type.isInstance( this ) ) {
			return type.cast( this );
		}

		if ( type.isInstance( persistenceContext ) ) {
			return type.cast( persistenceContext );
		}

		throw new PersistenceException(
				"Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'" );
	}

	@Override
	@Nonnull
	public Object getDelegate() {
		checkOpen();
		return this;
	}

	@Override
	@Nonnull
	public Collection<?> getManagedEntities() {
		checkSessionReentrancy();
		return persistenceContext.getEntityHoldersByKey()
				.values().stream().map( EntityHolder::getManagedObject )
				.toList();
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Unwrap only to types supported by Session/SessionImpl (e.g. Session.class, SessionImplementor.class, Connection via doWork).
  2. Cast or adapt manually if the target type is not a supported unwrap interface.

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java:2783 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/6c711b9c66a967a0. Report an issue: GitHub.