hibernate/hibernate-orm · error · ClassCastException

Object of type ' ' can't be cast to…

Error message

Object of type '{}' can't be cast to PersistentAttributeInterceptable

What it means

Cast helper converting an arbitrary object to PersistentAttributeInterceptable: the object does not expose that secondary supertype (it is not a lazy-attribute-instrumented entity). The faulty input is the object being cast; pass an enhanced entity instance.

Solutions

  1. Ensure the entity class is bytecode-enhanced or a proper managed type before casting to PersistentAttributeInterceptable
  2. Enable Hibernate bytecode enhancement in the build so managed types implement the interception interfaces
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/ManagedTypeHelper.java:262 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/2f186cc1eab2f5d3. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/ManagedTypeHelper.java:262

		void accept(SelfDirtinessTracker tracker, T optionalParam);
	}

	/**
	 * Cast the object to PersistentAttributeInterceptable
	 * (using this is highly preferrable over a direct cast)
	 * @param entity the entity to cast
	 * @return the same instance after casting
	 * @throws ClassCastException if it's not of the right type
	 */
	public static PersistentAttributeInterceptable asPersistentAttributeInterceptable(final Object entity) {
		Objects.requireNonNull( entity );
		if ( entity instanceof PrimeAmongSecondarySupertypes t ) {
			final PersistentAttributeInterceptable e = t.asPersistentAttributeInterceptable();
			if ( e != null ) {
				return e;
			}
		}
		throw new ClassCastException( "Object of type '" + entity.getClass() + "' can't be cast to PersistentAttributeInterceptable" );
	}

	public static PersistentAttributeInterceptable asPersistentAttributeInterceptableOrNull(final Object entity) {
		if ( entity instanceof PrimeAmongSecondarySupertypes t ) {
			return t.asPersistentAttributeInterceptable();
		}
		return null;
	}

	/**
	 * Cast the object to HibernateProxy
	 * (using this is highly preferrable over a direct cast)
	 * @param entity the entity to cast
	 * @return the same instance after casting
	 * @throws ClassCastException if it's not of the right type
	 */
	public static HibernateProxy asHibernateProxy(final Object entity) {
		Objects.requireNonNull( entity );

View on GitHub (pinned to fad1729dce)