hibernate/hibernate-orm · error · ClassCastException

incompatible with

Error message

{} incompatible with {}

What it means

Compatibility guard inside the ByteBuddy proxy's call path (invoke on the intercepted method): a runtime type check found the proxy's target/expected class and the actual implementation class incompatible (e.g. classloader or class-redefinition mismatch after hot redeploy). The faulty state is a stale proxy whose persistent class no longer matches the loaded implementation; discard old proxies/session factory state after reloading classes.

Solutions

  1. Ensure the interceptor's entity class matches the proxy's persistent class.
  2. Avoid mixing proxies created for different entity mappings.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/proxy/pojo/bytebuddy/ByteBuddyInterceptor.java:50

			SharedSessionContractImplementor session,
			boolean overridesEquals) {
		super( entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals );
		this.interfaces = interfaces;
	}

	@Override
	public Object intercept(Object proxy, Method method, Object[] args) throws Throwable {
		return invoke( method, args, proxy );
	}

	@Override
	protected Object call(Object proxy, Method method, Object[] args) throws Throwable {
		final Object target = getImplementation();
		final Object returnValue;
		try {
			if ( isPublic( persistentClass, method ) ) {
				if ( !method.getDeclaringClass().isInstance( target ) ) {
					throw new ClassCastException(
							target.getClass().getName()
							+ " incompatible with "
							+ method.getDeclaringClass().getName()
					);
				}
				returnValue = method.invoke( target, args );
			}
			else {
				method.setAccessible( true );
				returnValue = method.invoke( target, args );
			}

			if ( returnValue == target ) {
				final var returnValueClass = returnValue.getClass();
				if ( returnValueClass.isInstance( proxy ) ) {
					return proxy;
				}
				else {

View on GitHub (pinned to fad1729dce)