{"record":{"id":"a91559981f5c786b","repo":"hibernate/hibernate-orm","slug":"attribute-is-of-type-which-is-not-a-mana","errorCode":null,"errorMessage":"Attribute '{}' is of type '{}' which is not a managed type","messagePattern":"Attribute '(.+?)' is of type '(.+?)' which is not a managed type","errorType":"exception","errorClass":"CannotContainSubGraphException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java","lineNumber":235,"sourceCode":"\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\n\t@Override\n\tpublic String toString() {\n\t\treturn \"AttributeNode[\" + description() + \"]\";\n\t}\n\n\t@Override\n\tpublic void merge(@Nonnull AttributeNodeImplementor<J, E, K> that) {\n\t\tassert that.isMutable() == isMutable();\n\t\tassert that.getAttributeDescriptor() == attribute;","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/graph/internal/AttributeNodeImpl.java#L217-L253","documentation":"Creating a subgraph requires the target type to be a managed type (entity or embeddable): asManagedType() casts the DomainType to ManagedDomainType and throws CannotContainSubGraphException when it is a basic type. It is reached from the lenient addValueSubgraph() (which does not run checkToOne first) and from addKeySubgraph() when a Map's key type is basic.","triggerScenarios":"Calling addValueSubgraph() on a node whose value graph type is basic (String, enum, Instant), or addKeySubgraph() on a Map keyed by a basic type (Map<String, Setting>) — the SubGraphImpl creation fails in asManagedType because a basic domain type has no attributes to descend into.","commonSituations":"Graph builders that add subgraphs for every node; collections of basic values where developers expect nested attribute control; map-key subgraphs on String/enum keys.","solutions":["Add subgraphs only where the target is an entity or embeddable type","For collections of basic values, just add the attribute node — there is nothing nested to fetch","For Maps with basic keys, fetch the map eagerly instead of using addKeySubgraph()","Guard generic builders by checking that the relevant type is a ManagedType via the metamodel"],"exampleFix":"// before — key subgraph on a basic map key\ngraph.addAttributeNode(\"settings\")   // Map<String, Setting>\n     .addKeySubgraph();               // key type String is basic → CannotContainSubGraphException\n\n// after — key subgraphs need managed key types; basic keys: just fetch the map\ngraph.addAttributeNode(\"settings\");\n// Map<Language, Setting> with @Embeddable Language → addKeySubgraph() works","handlingStrategy":"type-guard","validationCode":"// For map keys: only managed key types can carry a key subgraph\nif (mm.managedType(Settings.class).getAttribute(\"settings\") instanceof MapAttribute<?, ?, ?> map\n        && isManagedTarget(map.getKeyType())) {\n    node.addKeySubgraph();\n}","typeGuard":"static boolean isManagedTarget(jakarta.persistence.metamodel.Type<?> type) {\n    return type instanceof jakarta.persistence.metamodel.ManagedType<?>;\n}","tryCatchPattern":null,"preventionTips":["Add subgraphs only when the target is an entity or embeddable","For basic-typed values or keys, control loading via the attribute node itself","In generic builders, check the target type with the metamodel before calling addValueSubgraph/addKeySubgraph"],"tags":["hibernate","entity-graph","subgraph","managed-type","basic-type","jpa"],"backgroundTag":"entity-graph-misuse","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}