Pergunta

can JPA ElementCollection Embeddable have ElementCollection List Embeddable property?

  @Entity
  class Person {
  @ElementCollection
    private List<Address> addressList;
  }


 @Embeddable
    public class Address {
      private String city;
      private String state;
      private String countary;
    @ElementCollection
    private List<Phone> phones;

      ...
    }
    @Embeddable
    public class Phone {
      private String type;
      private String areaCode;
      private String number;
      ...
    }
Foi útil?

Solução

Yes you can have that, if you try your configuration you will have something like this.

http://postimg.org/image/pc0f3cxbp/

Also Embeddable types can have Collection Relationship to another entities this allowed by JPA.

Taking from specs JSR317

An embeddable class may be used to represent the state of another embeddable class.

An embeddable class (including an embeddable class within another embeddable class) may contain a collection of a basic type or other embeddable class.

An embeddable class may contain a relationship to an entity or collection of entities. Since instances of embeddable classes themselves have no persistent identity, the relationship from the referenced entity is to the entity that contains the embeddable instance(s) and not to the embeddable itself.

An embeddable class that is used as an embedded id or as a map key must not contain such a relationship

UPDATE

According to your question if embeddables can contain a list of embeddables this is possible and can be done as picture in this answer suggest, BUT regarding if and ElementCollection can contain an embeddables with another element collection the answer is NO

Trying to do that will cause.

Mapping contains an embeddable "examples.model.Address" with a prohibited mapping "phones", element collections may not contain embeddables with element collection mappings

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top