{"record":{"id":"b267ff7db5ceb669","repo":"hibernate/hibernate-orm","slug":"attribute-is-not-a-to-one-association","errorCode":null,"errorMessage":"Attribute '{}' is not a to-one association","messagePattern":"Attribute '(.+?)' is not a to-one association","errorType":"exception","errorClass":"CannotContainSubGraphException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java","lineNumber":219,"sourceCode":"\t\tthrow new UnsupportedOperationException(\"Not a singular attribute node\");\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic SubGraphImplementor<E> addElementSubgraph() {\n\t\tthrow new UnsupportedOperationException( \"Not a collection-valued attribute node\" );\n\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\" );","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java#L201-L237","documentation":"Before creating a singular subgraph, checkToOne() verifies the attribute is a to-one association or an embedding (MANY_TO_ONE, ONE_TO_ONE or EMBEDDED). A subgraph needs a managed type to descend into; a basic value or a collection cannot provide one, so CannotContainSubGraphException is thrown.","triggerScenarios":"addSubgraph()/addSingularSubgraph() on a node whose attribute is a basic type (String, enum, Instant, int) — e.g. graph.addAttributeNode(\"version\").addSingularSubgraph(); also singular non-association attributes like a basic-type SingularAttribute.","commonSituations":"Graph-building helpers that call addSubgraph for every node; porting pre-Hibernate-6 code where subgraph() was accepted on collection attributes (now routed to addElementSubgraph); copying JPA 2-era examples onto Hibernate 6/7.","solutions":["Keep addSubgraph()/addSingularSubgraph() for to-one (@ManyToOne/@OneToOne) and @Embedded attributes only","For collection attributes use addElementSubgraph() (or addKeySubgraph() for map keys)","For basic attributes there is no subgraph — control their loading by adding/removing the attribute node itself","Update legacy subgraph() calls on collections to addElementSubgraph()"],"exampleFix":"// before — singular node for a basic attribute cannot carry a subgraph\ngraph.addAttributeNode(\"version\")   // basic int attribute\n     .addSingularSubgraph();         // → CannotContainSubGraphException\n\n// after — subgraphs belong to to-one/embedded attributes\ngraph.addAttributeNode(\"supplier\")  // @ManyToOne\n     .addSingularSubgraph()\n     .addAttributeNode(\"address\");","handlingStrategy":"type-guard","validationCode":"if (acceptsSingularSubgraph(attr)) {\n    node.addSingularSubgraph().addAttributeNode(\"address\");\n}","typeGuard":"static boolean acceptsSingularSubgraph(Attribute<?, ?> attr) {\n    return switch (attr.getPersistentAttributeType()) {\n        case MANY_TO_ONE, ONE_TO_ONE, EMBEDDED -> true;\n        default -> false;\n    };\n}","tryCatchPattern":null,"preventionTips":["Add singular subgraphs only to to-one and embedded attributes","Migrate pre-Hibernate-6 subgraph() calls on collections to addElementSubgraph()","Basic attributes are controlled by node presence, never by subgraphs"],"tags":["hibernate","entity-graph","subgraph","to-one","association","jpa"],"backgroundTag":"entity-graph-misuse","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}