hibernate/hibernate-orm · error · PersistenceException

Hibernate cannot unwrap '" + getClass().getName() + "' as…

Error message

Hibernate cannot unwrap '" + getClass().getName() + "' as '" + type.getName() + "'

What it means

Thrown by StatelessSessionImpl.unwrap when the requested type is not an instance of the session itself; stateless sessions expose no persistence context to unwrap.

Solutions

  1. Unwrap only to interfaces supported by StatelessSession (e.g. StatelessSession itself, SharedSessionContractImplementor); otherwise adapt manually.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java:1889 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/1a6510f8c859e73a. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java:1889

		return CacheLoadHelper.loadFromSecondLevelCache( this, instanceToLoad, lockMode, persister, entityKey );
	}

	@Override
	@Nonnull
	public TransactionCompletionCallbacksImplementor getTransactionCompletionCallbacksImplementor() {
		return transactionCompletionCallbacks.forSharing();
	}

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

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

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

}

View on GitHub (pinned to fad1729dce)