hibernate/hibernate-orm · error · IllegalArgumentException

Could not determine identifier of proxy passed to evict()

Error message

Could not determine identifier of proxy passed to evict()

What it means

onEvict received an uninitialized proxy whose lazy initializer exposes no internal identifier, so eviction cannot proceed without initializing it. The input at fault is the uninitialized proxy passed to Session.evict()/detach().

Solutions

  1. Ensure the proxy passed to Session.evict() is initialized (Hibernate.initialize) so its identifier can be determined before evicting.
  2. Fetch the entity by id and evict the loaded instance instead of an uninitialized proxy.
  3. Check that the proxy's session is still open when evict() is called.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown when evict() is called with a lazy proxy whose identifier cannot be determined.

Common situations: Typical situations: evicting an uninitialized proxy whose id was never assigned; a proxy created with a null id.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java:51

	 * Handle the given evict event.
	 *
	 * @param event The evict event to be handled.
	 *
	 */
	@Override
	public void onEvict(@Nonnull EvictEvent event) {
		final var source = event.getSession();
		final var persistenceContext = source.getPersistenceContextInternal();
		final Object object = event.getObject();
		//noinspection ConstantValue
		if ( object == null ) {
			throw new NullPointerException( "null passed to Session.evict()" );
		}
		final var lazyInitializer = extractLazyInitializer( object );
		if ( lazyInitializer != null ) {
			final Object id = lazyInitializer.getInternalIdentifier();
			if ( id == null ) {
				throw new IllegalArgumentException( "Could not determine identifier of proxy passed to evict()" );
			}
			final var persister =
					source.getFactory().getMappingMetamodel()
							.getEntityDescriptor( lazyInitializer.getEntityName() );
			final var key = source.generateEntityKey( id, persister );
			final var holder = persistenceContext.detachEntity( key );
			// if the entity has been evicted then its holder is null
			if ( holder != null && !lazyInitializer.isUninitialized() ) {
				final Object entity = holder.getEntity();
				if ( entity != null ) {
					final var entry = persistenceContext.removeEntry( entity );
					doEvict( entity, key, entry.getPersister(), event.getSession() );
				}
			}
			lazyInitializer.unsetSession();
		}
		else {
			final var entry = persistenceContext.getEntry( object );

View on GitHub (pinned to fad1729dce)