如何在JPA 2.0中定义包含@OneToMany关系的Embedded类型的ElementCollection

时间:2013-09-17 11:10:46

标签: jpa jpa-2.0

  

大家好,

     

我是JPA的新手。我在下面的情景中面临一个问题;

     

我有一个可嵌入的类ContactInformation,如下所述;

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

我有另一个实体类Employee,如下所示;

  
   @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;

    ...
    }
  
    

在上面的例子中,我在上面提到的行中得到了编译时错误,因为“Mapping包含一个带有禁止映射”phoneList“的嵌入式”main.ContractInformation“,元素集合中的嵌入可能只包含多个 - 一对一或一对一的映射,必须位于关系的“拥有”一侧,不得使用连接表“

  

你能帮我解决一下这个问题吗?

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

正如错误所述,embeddable不能使用需要外键指向可嵌入的映射,例如OneToMany或ManyToMany。这是因为embeddable没有映射所需的外键的主键。将嵌入设置为完整实体。

相关问题