{"record":{"id":"2b8eb6aa71b8f5b7","repo":"hibernate/hibernate-orm","slug":"attribute-is-annotated-bag-and-may-not-als","errorCode":null,"errorMessage":"Attribute '{}' is annotated '@Bag' and may not also be annotated '@OrderColumn'","messagePattern":"Attribute '(.+?)' is annotated '@Bag' and may not also be annotated '@OrderColumn'","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java","lineNumber":930,"sourceCode":"\t\t\tcase SET, ORDERED_SET -> new SetBinder( customTypeBeanAccess, false, buildingContext );\n\t\t\tcase SORTED_SET -> new SetBinder( customTypeBeanAccess, true, buildingContext );\n\t\t};\n\t}\n\n\tprivate static CollectionClassification determineCollectionClassification(\n\t\t\tMemberDetails property,\n\t\t\tMetadataBuildingContext buildingContext) {\n\t\tif ( property.isArray() ) {\n\t\t\treturn CollectionClassification.ARRAY;\n\t\t}\n\n\t\tfinal var modelsContext = buildingContext.getBootstrapContext().getModelsContext();\n\t\tif ( !property.hasAnnotationUsage( Bag.class, modelsContext ) ) {\n\t\t\treturn determineCollectionClassification( determineSemanticJavaType( property ), property, buildingContext );\n\t\t}\n\n\t\tif ( property.hasAnnotationUsage( OrderColumn.class, modelsContext ) ) {\n\t\t\tthrow new AnnotationException( \"Attribute '\"\n\t\t\t\t\t+ qualify( property.getDeclaringType().getName(), property.getName() )\n\t\t\t\t\t+ \"' is annotated '@Bag' and may not also be annotated '@OrderColumn'\" );\n\t\t}\n\n\t\tif ( property.hasAnnotationUsage( ListIndexBase.class, modelsContext ) ) {\n\t\t\tthrow new AnnotationException( \"Attribute '\"\n\t\t\t\t\t+ qualify( property.getDeclaringType().getName(), property.getName() )\n\t\t\t\t\t+ \"' is annotated '@Bag' and may not also be annotated '@ListIndexBase'\" );\n\t\t}\n\n\t\tfinal var collectionJavaType = property.getType().determineRawClass().toJavaClass();\n\t\tif ( java.util.List.class.equals( collectionJavaType )\n\t\t\t\t|| java.util.Collection.class.equals( collectionJavaType ) ) {\n\t\t\treturn CollectionClassification.BAG;\n\t\t}\n\t\telse {\n\t\t\tthrow new AnnotationException(\n\t\t\t\t\tString.format(","sourceCodeStart":912,"sourceCodeEnd":948,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java#L912-L948","documentation":"@Bag explicitly requests unordered BAG semantics, while @OrderColumn implies an ordered LIST backed by an index column. The two contradict each other, so CollectionBinder throws this AnnotationException when both are present on the same attribute.","triggerScenarios":"A property has @Bag and also hasAnnotationUsage(OrderColumn.class) during determineCollectionClassification; the check runs whenever @Bag is present.","commonSituations":"Adding @Bag to get bag semantics (deduplicated SQL handling, no index maintenance) on a mapping that already carried @OrderColumn; copy-paste of list mappings while switching to bags; version upgrades where legacy @LazyCollection(BAG) mappings were converted to @Bag.","solutions":["Remove @OrderColumn if you want bag semantics (unordered, no index column)","Or remove @Bag so the attribute maps as a LIST with @OrderColumn","If you only wanted a base for the index, use @ListIndexBase together with @OrderColumn, not @Bag"],"exampleFix":"// before\n@Bag\n@OrderColumn\nList<Item> items;   // error: bag + order column\n\n// after\n@OrderColumn\nList<Item> items;   // drop @Bag -> LIST with order column\n// or keep @Bag and remove @OrderColumn for true unordered bag semantics","handlingStrategy":"validation","validationCode":"// Reject @Bag combined with @OrderColumn or @ListIndexBase\nstatic void checkBagConflicts(Class<?>... entities) {\n    for ( Class<?> c : entities ) {\n        for ( Field f : c.getDeclaredFields() ) {\n            if ( f.isAnnotationPresent( Bag.class )\n                    && ( f.isAnnotationPresent( OrderColumn.class )\n                         || f.isAnnotationPresent( ListIndexBase.class ) ) ) {\n                throw new IllegalStateException( \"@Bag conflicts with an order/index annotation on \"\n                    + c.getName() + \".\" + f.getName() );\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide collection semantics once per attribute: bag (unordered) or list (indexed), never both","When migrating @LazyCollection(BAG) or legacy mappings to @Bag, strip @OrderColumn and @ListIndexBase","Add the reflection scan to the mapping test suite"],"tags":["hibernate","bag","order-column","collection-semantics","annotation-binding"],"backgroundTag":"bag-annotation-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}