{"record":{"id":"8dd1321a1a50de24","repo":"hibernate/hibernate-orm","slug":"bag-is-not-a-list-8dd132","errorCode":null,"errorMessage":"Bag is not a list: ","messagePattern":"Bag is not a list: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentIdentifierBag.java","lineNumber":99,"sourceCode":"\t * @param session The session\n\t * @param coll The base elements\n\t */\n\tpublic PersistentIdentifierBag(SharedSessionContractImplementor session, Collection<E> coll) {\n\t\tsuper( session );\n\t\tsetCollection( coll );\n\t\tsetInitialized();\n\t\tsetDirectlyAccessible( true );\n\t\tidentifiers = new HashMap<>();\n\t}\n\n\tprivate void setCollection(Collection<E> bag) {\n\t\tthis.collection = bag;\n\t\tthis.values = bag instanceof List<E> list ? list : null;\n\t}\n\n\tprotected List<E> bagAsList() {\n\t\tif ( values == null ) {\n\t\t\tthrow new IllegalStateException( \"Bag is not a list: \" + collection.getClass().getName() );\n\t\t}\n\t\treturn values;\n\t}\n\n\t@Override\n\tpublic void initializeFromCache(CollectionPersister persister, Object disassembled, Object owner)\n\t\t\tthrows HibernateException {\n\t\tfinal Serializable[] array = (Serializable[]) disassembled;\n\t\tfinal int size = array.length;\n\n\t\tassert identifiers == null;\n\t\tassert collection == null;\n\n\t\tidentifiers = new HashMap<>();\n\t\t//noinspection unchecked\n\t\tsetCollection( (Collection<E>) persister.getCollectionSemantics().instantiateRaw( size, persister ) );\n\n\t\tfor ( int i = 0; i < size; i+=2 ) {","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentIdentifierBag.java#L81-L117","documentation":"PersistentIdentifierBag (id-bag mapping) wraps any java.util.Collection; list-position operations go through bagAsList(), which only works when the wrapped collection is a List. If the id-bag was constructed around a non-List collection, list-style operations throw IllegalStateException with the collection's class name. As with PersistentBag, the runtime collection type does not support the list operations being attempted.","triggerScenarios":"An id-bag mapped field (collection with <id-bag> or identifier-bag semantics) whose application-initialized value is a non-List Collection such as a HashSet; then indexed add/get or list-snapshot paths run against it.","commonSituations":"Fields initialized with HashSet or custom Collection implementations under id-bag mappings; refactors that changed the field initializer but kept the mapping; copy-pasted initializers across entity classes.","solutions":["Declare the field as List and initialize with ArrayList for id-bag mappings","Map set-like fields as Set instead of id-bag if set semantics are intended","Avoid positional operations on identifier bags","Add a convention check/test that bag and id-bag fields are initialized with ArrayList"],"exampleFix":"// before\n@org.hibernate.annotations.CollectionId /* id-bag style mapping */\nprivate Collection<Tag> tags = new HashSet<>(); // non-List backing -> bagAsList() throws\n\n// after\n@org.hibernate.annotations.CollectionId /* id-bag style mapping */\nprivate List<Tag> tags = new ArrayList<>();","handlingStrategy":"type-guard","validationCode":"if (!(order.getTags() instanceof List)) {\n    throw new IllegalStateException(\n            \"tags is id-bag mapped around a non-List collection; use ArrayList\");\n}","typeGuard":"static boolean supportsIndexAccess(Collection<?> collection) {\n    return collection instanceof List;\n}","tryCatchPattern":null,"preventionTips":["Initialize id-bag mapped fields with ArrayList","Map set-like fields as Set rather than id-bag","Add a convention test that bag/id-bag fields are List-typed with ArrayList initializers"],"tags":["hibernate","collection-mapping","id-bag","list","mapping-mismatch"],"backgroundTag":"bag-backed-by-non-list-collection","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}