hibernate/hibernate-orm · error · IllegalArgumentException

Not an instance of CacheKeyImplementation{cacheKeyObject}

Error message

Not an instance of CacheKeyImplementation{cacheKeyObject}

What it means

Error "Not an instance of CacheKeyImplementation{cacheKeyObject}" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/cache/internal/DefaultCacheKeysFactory.java:105

	@Nonnull
	public static Object staticCreateNaturalIdKey(
			@Nonnull Object naturalIdValues,
			@Nonnull EntityPersister persister,
			@Nonnull SharedSessionContractImplementor session) {
		return NaturalIdCacheKey.from( naturalIdValues, persister, session );
	}

	@Nonnull
	public static Object staticGetEntityId(@Nonnull Object cacheKeyObject) {
		if ( cacheKeyObject instanceof BasicCacheKeyImplementation basicCacheKey ) {
			return basicCacheKey.id;
		}
		else if ( cacheKeyObject instanceof CacheKeyImplementation cacheKey) {
			return cacheKey.getId();
		}
		else {
			throw new IllegalArgumentException( "Not an instance of CacheKeyImplementation" + cacheKeyObject );
		}
	}

	@Nonnull
	public static Object staticGetCollectionId(@Nonnull Object cacheKey) {
		return staticGetEntityId( cacheKey );
	}

	@Nonnull
	public static Object staticGetNaturalIdValues(@Nonnull Object cacheKey) {
		return ( (NaturalIdCacheKey) cacheKey ).getNaturalIdValues();
	}

	@Override
	@Nonnull
	public Object createCollectionKey(
			@Nonnull Object id,
			@Nonnull CollectionPersister persister,

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not pass arbitrary objects as cache keys; use the keys produced by the Session/Cache API (CacheKeyImplementation instances).

Example fix

Do not pass arbitrary objects as cache keys; use the keys produced by the Session/Cache API (CacheKeyImplementation instances).

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/b92e8c327a30e43a. Report an issue: GitHub.