2013-05-14 87 views
0

我有一个问题(我不知道这是否是可能的)丝毫的关联,并使用JPA嵌入式ID @Embedded类...JPA @OneToMany关系作为@Id

我有一个Person类与它的ID:

@Entity 
public class Person{ 
    @EmbeddedId 
    private PersonCode personCode; 
    private String name; 

    @Embeddable 
    public static class PersonCode{ 
     private String code; 
    } 
} 

然后我想创建一个类Company与联想:

@Entity 
public class Company{ 
    private String name; 

    @OneToMany 
    private List<PersonCode> employees; 
} 

但是我有这样的例外:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: example.domain.Company.employees[example.domain.Person$PersonCode] 

回答

0

关联必须在两个实体之间。该公司应该有一个List<Person>

顺便说一句,你使它比需要更难。只需使用

@Entity 
public class Person{ 
    @Id 
    private String code; 

    private String name; 
} 

没有理由将单个字段包装到可嵌入类中。

+0

由于一些DDD reccomendation,我试图弄清楚如何管理两个实体的关系,而不是两者都不对另一个负责......所以如果有可能使用单个ID类来建立关系很容易实现......所以谢谢你! – rascio 2013-05-14 18:35:08