{"record":{"id":"1fb9e0628b60f03a","repo":"hibernate/hibernate-orm","slug":"property-belongs-to-an-embeddable-class-th","errorCode":null,"errorMessage":"Property '{}' belongs to an '@Embeddable' class that is contained in an '@ElementCollection' and may not be a {}","messagePattern":"Property '(.+?)' belongs to an '@Embeddable' class that is contained in an '@ElementCollection' and may not be a (.+?)","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java","lineNumber":434,"sourceCode":"\t}\n\n\tprivate static PropertyData virtualPropertyData(PropertyData inferredData, MemberDetails property) {\n\t\t//do not use \"element\" if you are a JPA 2 @ElementCollection, only for legacy Hibernate mappings\n\t\treturn property.hasDirectAnnotationUsage( ElementCollection.class )\n\t\t\t\t? inferredData\n\t\t\t\t: new WrappedInferredData(inferredData, \"element\" );\n\t}\n\n\tprivate static void checkAnnotations(\n\t\t\tPropertyHolder propertyHolder,\n\t\t\tPropertyData inferredData,\n\t\t\tMemberDetails property,\n\t\t\tOneToMany oneToMany,\n\t\t\tManyToMany manyToMany,\n\t\t\tElementCollection elementCollection) {\n\t\tif ( ( oneToMany != null || manyToMany != null || elementCollection != null )\n\t\t\t\t&& isToManyAssociationWithinEmbeddableCollection( propertyHolder ) ) {\n\t\t\tthrow new AnnotationException( \"Property '\" + getPath( propertyHolder, inferredData ) +\n\t\t\t\t\t\"' belongs to an '@Embeddable' class that is contained in an '@ElementCollection' and may not be a \"\n\t\t\t\t\t+ annotationName( oneToMany, manyToMany, elementCollection ));\n\t\t}\n\n\t\tif ( oneToMany != null && property.hasDirectAnnotationUsage( SoftDelete.class ) ) {\n\t\t\tthrow new UnsupportedMappingException(\n\t\t\t\t\t\"@SoftDelete cannot be applied to @OneToMany - \" +\n\t\t\t\t\t\t\tproperty.getDeclaringType().getName() + \".\" + property.getName()\n\t\t\t);\n\t\t}\n\n\t\tif ( property.hasDirectAnnotationUsage( OrderColumn.class )\n\t\t\t\t&& manyToMany != null\n\t\t\t\t&& isNotBlank( manyToMany.mappedBy() ) ) {\n\t\t\tthrow new AnnotationException(\"Collection '\" + getPath( propertyHolder, inferredData ) +\n\t\t\t\t\t\"' is the unowned side of a bidirectional '@ManyToMany' and may not have an '@OrderColumn'\");\n\t\t}\n","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java#L416-L452","documentation":"JPA forbids nested collection associations: an @Embeddable used as the element of an @ElementCollection may not itself declare @OneToMany, @ManyToMany, or @ElementCollection properties. CollectionBinder.checkAnnotations throws this AnnotationException, naming the offending annotation, because there is no valid mapping for a collection nested inside a collection element.","triggerScenarios":"An entity has an @ElementCollection of an @Embeddable type (or the property holder is otherwise within an embeddable collection), and a property of that embeddable carries @OneToMany, @ManyToMany, or @ElementCollection.","commonSituations":"Modeling nested collections like Order -> @ElementCollection Set<Address> -> List<Phone> inside Address; refactoring that moves an association into an embeddable that is also used as an element-collection element; reusing an embeddable in two contexts, one of which is an element collection.","solutions":["Promote the nested association to the owning entity: put the @OneToMany directly on the entity instead of inside the embeddable","Model the nested data as a separate @Entity linked by a normal association from the owner","Flatten the embeddable: remove the collection property and store repeated values through a real entity or a JSON column"],"exampleFix":"// before\n@Entity\nclass Customer {\n    @ElementCollection\n    Set<Address> addresses;   // Address is @Embeddable\n}\n@Embeddable\nclass Address {\n    @OneToMany(mappedBy = \"address\")   // error: nested to-many inside element-collection embeddable\n    List<Phone> phones;\n}\n\n// after\n@Entity\nclass Customer {\n    @ElementCollection\n    Set<Address> addresses;\n\n    @OneToMany(mappedBy = \"customer\")   // association lives on the owning entity\n    List<Phone> phones;\n}","handlingStrategy":"validation","validationCode":"// Reject to-many/@ElementCollection properties inside embeddables used as element-collection elements\nstatic void checkNoNestedCollections(Class<?> entity, Class<? extends Annotation> elementAnn,\n                                     Class<?> embeddable) {\n    if ( entity.isAnnotationPresent( elementAnn ) ) {\n        for ( Field f : embeddable.getDeclaredFields() ) {\n            if ( f.isAnnotationPresent( OneToMany.class )\n                    || f.isAnnotationPresent( ManyToMany.class )\n                    || f.isAnnotationPresent( ElementCollection.class ) ) {\n                throw new IllegalStateException( \"Nested collection inside element-collection embeddable: \"\n                    + embeddable.getName() + \".\" + f.getName() );\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep embeddables value-like: no associations to collections, only scalar/basic values","Model nested structures as separate entities with explicit associations from the owner","Before reusing an existing embeddable inside an @ElementCollection, audit it for to-many properties"],"tags":["hibernate","jpa","element-collection","embeddable","nested-collection","annotation-binding"],"backgroundTag":"nested-collection-mapping","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}