hibernate/hibernate-orm · error · ClassCastException

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

Error message

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

What it means

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

Source

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

		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 );
		if ( entity instanceof PrimeAmongSecondarySupertypes t ) {
			final HibernateProxy e = t.asHibernateProxy();
			if ( e != null ) {
				return e;
			}
		}
		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" );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not cast arbitrary managed objects to HibernateProxy; check instanceof HibernateProxy first
  2. Use Hibernate.unproxy() utilities to obtain the underlying entity safely

When it happens

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