{"record":{"id":"497111ab18111523","repo":"hibernate/hibernate-orm","slug":"replacement-of-not-directly-accessible-collection","errorCode":null,"errorMessage":"Replacement of not directly accessible collection found: {role}","messagePattern":"Replacement of not directly accessible collection found: (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java","lineNumber":1042,"sourceCode":"\t\tfinal Object key = collection.getKey();\n\t\tassert key != null;\n\t\tfinal var collectionEntry = new CollectionEntry( persister, key );\n\t\taddCollection( collection, collectionEntry, key );\n\t\tif ( session.getLoadQueryInfluencers().effectivelyBatchLoadable( persister ) ) {\n\t\t\tgetBatchFetchQueue().addBatchLoadableCollection( collection, collectionEntry );\n\t\t}\n\t}\n\n\t@Override\n\tpublic void addNewCollection(CollectionPersister persister, PersistentCollection<?> collection)\n\t\t\tthrows HibernateException {\n\t\taddCollection( collection, persister );\n\t}\n\n\t@Override\n\tpublic void replaceCollection(CollectionPersister persister, PersistentCollection<?> oldCollection, PersistentCollection<?> collection) {\n\t\tif ( !oldCollection.isDirectlyAccessible() ) {\n\t\t\tthrow new HibernateException( \"Replacement of not directly accessible collection found: \"\n\t\t\t\t\t\t\t\t\t\t\t+ oldCollection.getRole() );\n\t\t}\n\t\tassert !collection.isDirectlyAccessible();\n\t\tfinal var oldEntry = collectionEntries.remove( oldCollection.$$_hibernate_getInstanceId(), oldCollection );\n\t\tfinal var entry =\n\t\t\t\toldEntry.getLoadedPersister() != null\n\t\t\t\t\t\t// This is an already existing/loaded collection so ensure the loadedPersister is initialized\n\t\t\t\t\t\t? new CollectionEntry( collection, session.getFactory() )\n\t\t\t\t\t\t// A newly wrapped collection\n\t\t\t\t\t\t: new CollectionEntry( persister, collection );\n\t\tentry.setReadOnly( oldEntry.isReadOnly(), collection );\n\t\tputCollectionEntry( collection, entry );\n\t\tfinal Object key = collection.getKey();\n\t\tif ( key != null ) {\n\t\t\tfinal var collectionKey = session.generateCollectionKey( entry.getLoadedPersister(), key );\n\t\t\tfinal var old = addCollectionByKey( collectionKey, collection );\n\t\t\tif ( old == null ) {\n\t\t\t\tthrow new HibernateException( \"No collection for replacement found: \" + collectionKey.getRole() );","sourceCodeStart":1024,"sourceCodeEnd":1060,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L1024-L1060","documentation":"Error \"Replacement of not directly accessible collection found: {role}\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1042 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not replace a collection that is not directly accessible through its owner; access and modify it via the owning entity","Fetch the collection through the owner before replacing it"],"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"}