sauumum
sauumum

Reputation: 1788

How to define a ElementCollection of an Embedded type which contains @OneToMany relation in JPA 2.0

Hi All,

I am new to JPA. I am facing an issue in below scenario;

I have an embeddable class ContactInformation as mention below;

@Embeddable public class ContactInformation {
    @OneToMany
    private Set<Phone> phoneList;
    @Embedded
    private Address address;
......
}

I have another entity class Employee as shown below;

   @Entity

   @IdClass(EmployeeId.class)

   public class Employee implements Serializable {


    private static final long serialVersionUID = 1L;

   @Id
   private String id;

   @Id
   private String name;

   @ElementCollection
   @CollectionTable(name = "employee_interests") 
   private Set<String> interests;
//COMPILE TIME ERROR LINE BELOW
   @ElementCollection
   private Set<ContractInformation> info;

    ...
    }

In above case, I am getting compile time error in above mention line as "Mapping contains an embeddable "main.ContractInformation" with a prohibited mapping "phoneList", embeddables in element collections may only contain many-to-one or one-to-one mappings which must be on the "owning" side of the relationship and must not use join table"

Can you please help me how we can correct this?

Thanks for your help in advance !!

Upvotes: 1

Views: 3303

Answers (1)

Chris
Chris

Reputation: 21145

As the error states, an embeddable cannot use a mapping that requires a foreign key to point at the embeddable, such as a OneToMany or ManyToMany. Thisis because the embeddable doesn't have a primary key for the foreign keys required by the mappings to point to. Make your embeddable a full entity instead.

Upvotes: 1

Related Questions