{"record":{"id":"97decb435025a1d0","repo":"hibernate/hibernate-orm","slug":"queued-clear-cannot-be-used-with-orphan-delete","errorCode":null,"errorMessage":"queued clear cannot be used with orphan delete","messagePattern":"queued clear cannot be used with orphan delete","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java","lineNumber":675,"sourceCode":"\t@Override\n\tpublic int hashCode() {\n\t\treturn super.hashCode();\n\t}\n\n\tfinal class Clear implements DelayedOperation<E> {\n\t\t@Override\n\t\tpublic void operate() {\n\t\t\tcollection.clear();\n\t\t}\n\n\t\t@Override\n\t\tpublic E getAddedInstance() {\n\t\t\treturn null;\n\t\t}\n\n\t\t@Override\n\t\tpublic E getOrphan() {\n\t\t\tthrow new UnsupportedOperationException( \"queued clear cannot be used with orphan delete\" );\n\t\t}\n\t}\n\n\tfinal class SimpleAdd extends AbstractValueDelayedOperation {\n\n\t\tpublic SimpleAdd(E addedValue) {\n\t\t\tsuper( addedValue, null );\n\t\t}\n\n\t\t@Override\n\t\tpublic void operate() {\n\t\t\t// Delayed operations only work on inverse collections i.e. collections with mappedBy,\n\t\t\t// and these collections don't have duplicates by definition.\n\t\t\t// Since cascading also operates on delayed operation's elements,\n\t\t\t// it can happen that an element is already associated with the collection after cascading,\n\t\t\t// but the queued operations are still executed after the lazy initialization of the collection.\n\t\t\t// To avoid duplicates, we have to check if the bag already contains this element\n\t\t\tif ( !collection.contains( getAddedInstance() ) ) {","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentBag.java#L657-L693","documentation":"Operations on an uninitialized collection are queued and replayed at initialization; ClearDelayedOperation records a wholesale clear. Queued operations must expose added/orphan elements for orphan-delete cascades, but a bulk clear cannot enumerate individual orphans, so getOrphan throws UnsupportedOperationException. The error appears when a lazy bag with orphanRemoval is cleared before initialization and the flush then asks the queued clear for its orphans.","triggerScenarios":"Calling collection.clear() on an uninitialized @OneToMany(orphanRemoval = true) bag and then flushing or merging; detached entities whose lazy inverse collections are cleared before reattachment; test fixtures rewriting collections wholesale before load.","commonSituations":"'Replace all children' implemented as clear() plus addAll() on lazy inverse collections; orphanRemoval enabled for convenience on bags; merge flows over detached graphs.","solutions":["Initialize the collection before clearing: Hibernate.initialize(owner.getItems()) inside the open session","Implement replacement as explicit remove/add on an initialized collection, or delete children via query","Reconsider orphanRemoval on inverse collections that are routinely cleared wholesale","Upgrade Hibernate - orphan handling for queued operations has improved across releases"],"exampleFix":"// before\norder.getLines().clear();      // uninitialized bag + orphanRemoval = true\norder.getLines().addAll(newLines);\n\n// after\nHibernate.initialize(order.getLines()); // inside the session\norder.getLines().clear();\norder.getLines().addAll(newLines);","handlingStrategy":"validation","validationCode":"if (order.getLines() instanceof PersistentCollection pc && !pc.wasInitialized()) {\n    Hibernate.initialize(order.getLines()); // queued clear() is unsafe with orphanRemoval\n}\norder.getLines().clear();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize lazy collections before structural rewrites like clear()","Prefer explicit child deletion for orphanRemoval collections over clear-and-refill","Guard 'clear + refill' helper methods with an initialization check"],"tags":["hibernate","collection-mapping","bag","orphan-removal","queued-operations"],"backgroundTag":"orphan-removal-queued-clear","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}