{"record":{"id":"bbd383ebf75a79b1","repo":"hibernate/hibernate-orm","slug":"found-shared-references-to-a-collection-role","errorCode":null,"errorMessage":"Found shared references to a collection: {role}","messagePattern":"Found shared references to a collection: (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java","lineNumber":1080,"sourceCode":"\t\t}\n\t}\n\n\t/**\n\t * Add a collection to the cache, with a given collection entry.\n\t *\n\t * @param coll The collection for which we are adding an entry.\n\t * @param entry The entry representing the collection.\n\t * @param key The key of the collection's entry.\n\t */\n\tprivate void addCollection(PersistentCollection<?> coll, CollectionEntry entry, Object key) {\n\t\tputCollectionEntry( coll, entry );\n\t\tfinal var collectionKey = session.generateCollectionKey( entry.getLoadedPersister(), key );\n\t\tfinal var old = addCollectionByKey( collectionKey, coll );\n\t\tif ( old != null ) {\n\t\t\tif ( old == coll ) {\n\t\t\t\tthrow new AssertionFailure( \"bug adding collection twice\" );\n\t\t\t}\n\t\t\tthrow new HibernateException(\n\t\t\t\t\t\"Found shared references to a collection: \" + collectionKey.getRole()\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate void putCollectionEntry(PersistentCollection<?> collection, CollectionEntry entry) {\n\t\tif ( collectionEntries == null ) {\n\t\t\tcollectionEntries = new InstanceIdentityMap<>();\n\t\t}\n\t\tassert collection.$$_hibernate_getInstanceId() == 0;\n\t\tcollection.$$_hibernate_setInstanceId( nextCollectionInstanceId() );\n\t\tcollectionEntries.put( collection, entry );\n\t}\n\n\t/**\n\t * Add a collection to the cache, creating a new collection entry for it\n\t *\n\t * @param collection The collection for which we are adding an entry.","sourceCodeStart":1062,"sourceCodeEnd":1098,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L1062-L1098","documentation":"Error \"Found shared references to a collection: {role}\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java:1080 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not share the same collection instance between two owners; create a separate collection per owner","Clone the collection contents into a new collection for the second owner"],"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"}