hibernate/hibernate-orm · error · HibernateException

Collection was evicted

Error message

Collection was evicted

What it means

Error "Collection was evicted" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java:37

import static org.hibernate.pretty.MessageHelper.collectionInfoString;
import jakarta.annotation.Nonnull;

/**
 * @author Gavin King
 */
public class DefaultInitializeCollectionEventListener implements InitializeCollectionEventListener {

	/**
	 * called by a collection that wants to initialize itself
	 */
	@Override
	public void onInitializeCollection(@Nonnull InitializeCollectionEvent event) {
		final var collection = event.getCollection();
		final var source = event.getSession();
		final var persistenceContext = source.getPersistenceContextInternal();
		final var collectionEntry = persistenceContext.getCollectionEntry( collection );
		if ( collectionEntry == null ) {
			throw new HibernateException( "Collection was evicted" );
		}
		if ( !collection.wasInitialized() ) {
			final var loadedPersister = collectionEntry.getLoadedPersister();
			checkPersister( collection, loadedPersister );
			final Object loadedKey = collectionEntry.getLoadedKey();
			if ( EVENT_LISTENER_LOGGER.isTraceEnabled() ) {
				EVENT_LISTENER_LOGGER.initializingCollection(
						collectionInfoString( loadedPersister, collection, loadedKey, source ) );
			}

			assert loadedPersister != null
				&& loadedKey != null;
			final boolean foundInCache = initializeFromCache( loadedKey, loadedPersister, collection, source );
			if ( foundInCache ) {
				EVENT_LISTENER_LOGGER.collectionInitializedFromCache();
			}
			else {
				EVENT_LISTENER_LOGGER.collectionNotCached();

View on GitHub (pinned to fad1729dce)

Solutions

  1. Avoid evicting collections (or their owners) from the persistence context while they are still being initialized.
  2. Do not call Session.clear()/evict() during lazy collection initialization; re-fetch the entity after clearing the session.

When it happens

Trigger: Thrown when a collection is accessed after its entry was evicted from the persistence context.

Common situations: Typical situations: session.evict() on the owning entity followed by lazy collection access; cache/persistence-context eviction during initialization.


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