{"record":{"id":"80c4a577e68cb225","repo":"hibernate/hibernate-orm","slug":"cannot-lazily-initialize-collection-collection-is","errorCode":null,"errorMessage":"Cannot lazily initialize collection (collection is being removed)","messagePattern":"Cannot lazily initialize collection \\(collection is being removed\\)","errorType":"exception","errorClass":"LazyInitializationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java","lineNumber":672,"sourceCode":"\t\t\tthrowLazyInitializationException( \"session is disconnected\" );\n\t\t}\n\t}\n\n\tprivate void throwLazyInitializationException(String message) {\n\t\tfinal var error = new StringBuilder( \"Cannot lazily initialize collection\" );\n\t\tif ( role != null ) {\n\t\t\terror.append( \" of role '\" ).append( role ).append( \"'\" );\n\t\t}\n\t\tif ( key != null ) {\n\t\t\terror.append( \" with key '\" ).append( key ).append( \"'\" );\n\t\t}\n\t\terror.append( \" (\" ).append( message ).append( \")\" );\n\t\tthrow new LazyInitializationException( error.toString() );\n\t}\n\n\tpublic static void checkPersister(PersistentCollection<?> collection, CollectionPersister persister) {\n\t\tif ( !collection.wasInitialized() && persister == null ) {\n\t\t\tthrow new LazyInitializationException( \"Cannot lazily initialize collection\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t+ \" (collection is being removed)\" );\n\t\t}\n\t}\n\n\tprotected final void setInitialized() {\n\t\tthis.initializing = false;\n\t\tthis.initialized = true;\n\t}\n\n\t@Override\n\tpublic boolean isInitializing() {\n\t\treturn initializing;\n\t}\n\n\tprotected final void setDirectlyAccessible(boolean directlyAccessible) {\n\t\tthis.directlyAccessible = directlyAccessible;\n\t}\n","sourceCodeStart":654,"sourceCodeEnd":690,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java#L654-L690","documentation":"checkPersister runs while Hibernate initializes a collection and verifies a CollectionPersister is available. An uninitialized collection with a null persister only occurs when the collection is being torn down (its owner is being removed), so attempting initialization then fails with LazyInitializationException. It typically surfaces during delete or orphan-removal flushes over lazy, uninitialized collections.","triggerScenarios":"Deleting an entity whose lazy collection is uninitialized while cascade processing needs its snapshot; @OneToMany(orphanRemoval = true) cascades forcing collection access during remove; merging or deleting detached instances loaded by another session.","commonSituations":"Cascade delete with orphanRemoval on lazy inverse collections; deleting detached graphs; edge cases in delete paths that are fixed in specific Hibernate versions.","solutions":["Initialize the collection before deleting: Hibernate.initialize(owner.getItems()) inside the open session","Load the entity fresh in the deleting session (session.get(...)) and then remove it","Trim cascade and orphanRemoval scope to associations that genuinely need delete semantics","If the mapping is clean and reproducible, check the Hibernate JIRA for your version and upgrade"],"exampleFix":"// before\nem.remove(em.getReference(Order.class, id)); // lazy lines uninitialized -> throws during flush\n\n// after\nOrder o = em.find(Order.class, id);\nHibernate.initialize(o.getLines());\nem.remove(o);","handlingStrategy":"validation","validationCode":"Order o = em.find(Order.class, id);\nif (o.getLines() instanceof PersistentCollection pc && !pc.wasInitialized()) {\n    Hibernate.initialize(o.getLines());\n}\nem.remove(o);","typeGuard":null,"tryCatchPattern":"try {\n    em.remove(order);\n} catch (LazyInitializationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"being removed\")) {\n        Hibernate.initialize(order.getLines()); // reattach+init, then remove again\n        em.remove(order);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Initialize collections that delete cascades will inspect before calling remove","Prefer removing fully loaded entities","Keep Hibernate updated - delete-path lazy handling has had fixes"],"tags":["hibernate","lazy-loading","cascade","orphan-removal","delete"],"backgroundTag":"lazyinitializationexception","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}