hibernate/hibernate-orm · error · PersistenceException

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

Error message

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

What it means

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

Source

Thrown at hibernate-core/src/main/java/org/hibernate/cache/internal/EnabledCaching.java:520

		getRegion( regionName ).clear();
		final var queryResultsRegionWithDuplicateName =
				queryResultsRegionsByDuplicateName.get( regionName );
		if ( queryResultsRegionWithDuplicateName != null ) {
			queryResultsRegionWithDuplicateName.clear();
		}
	}

	@Override
	@Nonnull
	public <T> T unwrap(@Nonnull Class<T> type) {
		if ( type.isAssignableFrom( EnabledCaching.class ) ) {
			return type.cast( this );
		}
		else if ( type.isAssignableFrom( RegionFactory.class ) ) {
			return type.cast( regionFactory );
		}
		else {
			throw new PersistenceException( "Hibernate cannot unwrap Cache as '" + type.getName() + "'" );
		}
	}

	@Override
	public void close() {
		for ( var region : regionsByName.values() ) {
			region.destroy();
		}
		for ( var region : queryResultsRegionsByDuplicateName.values() ) {
			region.destroy();
		}
	}


	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// JPA-defined methods

	@Override

View on GitHub (pinned to fad1729dce)

Solutions

  1. Unwrap the Cache to a type it actually implements (e.g. the provider's region type), or check that the expected cache provider is configured.

Example fix

Unwrap the Cache to a type it actually implements (e.g. the provider's region type), or check that the expected cache provider is configured.

When it happens

Trigger: Second-level caching is misconfigured or a cache region/key does not match what Hibernate expects.

Common situations: Enabling @Cacheable entities without setting hibernate.cache.region.factory_class, custom RegionFactory implementations, or unwrapping Cache to provider types.


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