I'm currently implementing JPA persistence with Spring Data and I'm not able to solve the following problem so far:
The problem
I have an @Entity EntityClass which has a Map<String, @Embedable MyEmbeddableClass>. This should be simple but MyEmbeddableClass owns its own Map<@Embeddable, Embeddable. Can I achieve this using the @ElementCollection annotation? In other words, can @ElementCollection members own their own collections?
@Entity
public class SimpleClass {
...
@Id
private Integer id
@ElementCollection
@CollectionTable(
name="simple_class_map",
joinColumns = {@JoinColumn(name="class_key)}
)
private Map<String, MyEmbeddableClass> firstMap;
}
@Embeddable
public class MyEmbeddableClass {
...
@ElementCollection
@CollectionTable(
name="simple_class_map",
joinColumns = {@JoinColumn(name="class_key)}
)
private Map<OtherEmbeddableClass, Object> secondMap;
}
@Embeddable
public class OtherEmbeddableClass {
private String text;
private String anotherText;
}
CodePudding user response:
No, a member of an @ElementCollection
cannot contain another @ElementCollection
(at least with Hibernate ORM).
This is mentioned in the documentation of Hibernate ORM:
Collections of value type include basic and embeddable types. Collections cannot be nested, and, when used in collections, embeddable types are not allowed to define other collections.
I don't think other JPA implementations support it either (but I might be wrong).