hibernate/hibernate-orm · error · ClassCastException

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

Error message

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

What it means

Error "Object of type '{}' can't be cast to ManagedEntity" thrown in hibernate/hibernate-orm.

Source

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

		throw new ClassCastException( "Object of type '" + entity.getClass() + "' can't be cast to HibernateProxy" );
	}

	/**
	 * Cast the object to ManagedEntity
	 * (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 ManagedEntity asManagedEntity(final Object entity) {
		Objects.requireNonNull( entity );
		if ( entity instanceof PrimeAmongSecondarySupertypes t ) {
			final ManagedEntity e = t.asManagedEntity();
			if ( e != null ) {
				return e;
			}
		}
		throw new ClassCastException( "Object of type '" + entity.getClass() + "' can't be cast to ManagedEntity" );
	}

	/**
	 * Cast the object to CompositeTracker
	 * (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 CompositeTracker asCompositeTracker(final Object entity) {
		Objects.requireNonNull( entity );
		if ( entity instanceof PrimeAmongSecondarySupertypes t ) {
			final CompositeTracker e = t.asCompositeTracker();
			if ( e != null ) {
				return e;
			}
		}
		throw new ClassCastException( "Object of type '" + entity.getClass() + "' can't be cast to CompositeTracker" );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Ensure bytecode enhancement is enabled so entities implement ManagedEntity
  2. Guard the cast with instanceof ManagedTypeHelper.asManagedEntity instead of a direct cast

When it happens

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