{"record":{"id":"d205c872929cd42d","repo":"hibernate/hibernate-orm","slug":"collection-was-evicted","errorCode":null,"errorMessage":"Collection was evicted","messagePattern":"Collection was evicted","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java","lineNumber":37,"sourceCode":"import static org.hibernate.pretty.MessageHelper.collectionInfoString;\nimport jakarta.annotation.Nonnull;\n\n/**\n * @author Gavin King\n */\npublic class DefaultInitializeCollectionEventListener implements InitializeCollectionEventListener {\n\n\t/**\n\t * called by a collection that wants to initialize itself\n\t */\n\t@Override\n\tpublic void onInitializeCollection(@Nonnull InitializeCollectionEvent event) {\n\t\tfinal var collection = event.getCollection();\n\t\tfinal var source = event.getSession();\n\t\tfinal var persistenceContext = source.getPersistenceContextInternal();\n\t\tfinal var collectionEntry = persistenceContext.getCollectionEntry( collection );\n\t\tif ( collectionEntry == null ) {\n\t\t\tthrow new HibernateException( \"Collection was evicted\" );\n\t\t}\n\t\tif ( !collection.wasInitialized() ) {\n\t\t\tfinal var loadedPersister = collectionEntry.getLoadedPersister();\n\t\t\tcheckPersister( collection, loadedPersister );\n\t\t\tfinal Object loadedKey = collectionEntry.getLoadedKey();\n\t\t\tif ( EVENT_LISTENER_LOGGER.isTraceEnabled() ) {\n\t\t\t\tEVENT_LISTENER_LOGGER.initializingCollection(\n\t\t\t\t\t\tcollectionInfoString( loadedPersister, collection, loadedKey, source ) );\n\t\t\t}\n\n\t\t\tassert loadedPersister != null\n\t\t\t\t&& loadedKey != null;\n\t\t\tfinal boolean foundInCache = initializeFromCache( loadedKey, loadedPersister, collection, source );\n\t\t\tif ( foundInCache ) {\n\t\t\t\tEVENT_LISTENER_LOGGER.collectionInitializedFromCache();\n\t\t\t}\n\t\t\telse {\n\t\t\t\tEVENT_LISTENER_LOGGER.collectionNotCached();","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java#L19-L55","documentation":"Error \"Collection was evicted\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when a collection is accessed after its entry was evicted from the persistence context.","commonSituations":"Typical situations: session.evict() on the owning entity followed by lazy collection access; cache/persistence-context eviction during initialization.","solutions":["Avoid evicting collections (or their owners) from the persistence context while they are still being initialized.","Do not call Session.clear()/evict() during lazy collection initialization; re-fetch the entity after clearing the session."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}