{"record":{"id":"c1c28da8836000ab","repo":"hibernate/hibernate-orm","slug":"attribute-is-not-a-to-many-association","errorCode":null,"errorMessage":"Attribute '{}' is not a to-many association","messagePattern":"Attribute '(.+?)' is not a to-many association","errorType":"exception","errorClass":"CannotContainSubGraphException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java","lineNumber":226,"sourceCode":"\t}\n\n\t@Override\n\t@Nonnull\n\tpublic SubGraphImplementor<K> addKeySubgraph() {\n\t\tthrow new UnsupportedOperationException( \"Not a Map-valued attribute node\" );\n\t}\n\n\tprotected void checkToOne() {\n\t\tfinal Attribute.PersistentAttributeType attributeType = attribute.getPersistentAttributeType();\n\t\tif ( attributeType != MANY_TO_ONE && attributeType != ONE_TO_ONE && attributeType != EMBEDDED ) {\n\t\t\tthrow new CannotContainSubGraphException( \"Attribute '\" + attribute.getName() + \"' is not a to-one association\" );\n\t\t}\n\t}\n\n\tprotected void checkToMany() {\n\t\tfinal Attribute.PersistentAttributeType attributeType = attribute.getPersistentAttributeType();\n\t\tif ( attributeType != MANY_TO_MANY && attributeType != ONE_TO_MANY ) {\n\t\t\tthrow new CannotContainSubGraphException( \"Attribute '\" + attribute.getName() + \"' is not a to-many association\" );\n\t\t}\n\t}\n\n\tprotected <T> ManagedDomainType<T> asManagedType(DomainType<T> domainType) {\n\t\tif ( domainType instanceof ManagedDomainType<T> managedDomainType ) {\n\t\t\treturn managedDomainType;\n\t\t}\n\t\telse {\n\t\t\tthrow new CannotContainSubGraphException( \"Attribute '\" + description()\n\t\t\t\t\t+ \"' is of type '\" + domainType.getTypeName()\n\t\t\t\t\t+ \"' which is not a managed type\" );\n\t\t}\n\t}\n\n\tprivate String description() {\n\t\treturn attribute.getDeclaringType().getTypeName() + \".\" + attribute.getName();\n\t}\n","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java#L208-L244","documentation":"Before creating an element subgraph, checkToMany() requires the attribute to be a to-many association (ONE_TO_MANY or MANY_TO_MANY). Note that @ElementCollection attributes (PersistentAttributeType.ELEMENT_COLLECTION) are rejected too, even when their elements are managed types — only entity-to-entity collections pass.","triggerScenarios":"addElementSubgraph() on an attribute that is not ONE_TO_MANY/MANY_TO_MANY: a basic attribute, a @ManyToOne, or an @ElementCollection (element type basic or embeddable).","commonSituations":"Trying to fetch @ElementCollection contents through an element subgraph; generic graph builders applying element subgraphs everywhere; associations refactored between element collections and one-to-many sets.","solutions":["Use addElementSubgraph() only on @OneToMany/@ManyToMany attributes","For @ElementCollection, just add the attribute node (it fetches eagerly within the graph); if per-element subgraphs are truly needed, remap the part as @OneToMany to an entity","For to-one attributes use addSingularSubgraph(); for basics no subgraph exists"],"exampleFix":"// before — element collection is not a to-many association\ngraph.addAttributeNode(\"tags\")     // @ElementCollection\n     .addElementSubgraph();         // → CannotContainSubGraphException\n\n// after — the collection is fetched by being in the graph; remap to @OneToMany if you need element subgraphs\ngraph.addAttributeNode(\"tags\");","handlingStrategy":"type-guard","validationCode":"if (acceptsElementSubgraph(attr)) {\n    node.addElementSubgraph().addAttributeNode(\"product\");\n}","typeGuard":"static boolean acceptsElementSubgraph(Attribute<?, ?> attr) {\n    return switch (attr.getPersistentAttributeType()) {\n        case ONE_TO_MANY, MANY_TO_MANY -> true;\n        default -> false;   // note: ELEMENT_COLLECTION is rejected by Hibernate here\n    };\n}","tryCatchPattern":null,"preventionTips":["Use element subgraphs only on @OneToMany/@ManyToMany associations","Remember @ElementCollection does not qualify for element subgraphs even with embeddable elements","Remap to @OneToMany when per-element subgraph control is required"],"tags":["hibernate","entity-graph","element-subgraph","to-many","collections","jpa"],"backgroundTag":"entity-graph-misuse","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}