JPA - 带有@ManytoOne的EmbeddedId

时间:2012-03-14 15:41:31

标签: java jpa maven

我的代码存在问题(显然),在互联网上进行多次搜索后,我找不到问题的答案,所以我在这里提问。 我有这个:

@Entity
public class Resident
{
    /** Attributes */
    @EmbeddedId
    private IdResident idResident;
     ...

@Embeddable
public class IdResident {
    @Column(name="NOM")
    private String nom;
    @ManyToOne
    @JoinColumn(name="CODE")
    private Port port;
  ...

@Entity
public class Port
{
    /** Attributes */
    @Id
    @Column(name="CODE")
    private String code;
    @Column(name="NOM")
    private String nom;
    ...

我正在使用Maven,我在persistence.xml中写了这个:

<class>beans.Port</class>
<class>beans.Resident</class>   

但是当我运行程序时,无论我写什么,我都有:

Exception Description: The mapping [port] from the embedded ID class 
[class beans.IdResident] is an invalid mapping for this class. An embeddable class that
 is used with an embedded ID specification (attribute [idResident] from the source 
[class beans.Resident]) can only contain basic mappings. Either remove the non
 basic mapping or change the embedded ID specification on the source to be embedded.

我不知道我的错误在哪里,我认为这是因为IdResident类中有一个Entity对象,但我不知道如何将其搞定

2 个答案:

答案 0 :(得分:5)

您得到的错误消息很好地解释了它,用作嵌入式ID的Embeddable只能包含基本映射,而不能包含关系。在JPA 2.0规范中,用以下词语告知:

  

嵌入式id类中定义的关系映射不是   支撑。

只需定义属于嵌入式id的嵌入式复合id的一部分的属性,以及实体本身的映射关系(或另一个嵌入式和包含@Embedded的映射)。

答案 1 :(得分:0)

在我看来,这是基于IdResident类中的ManyToOne映射导致错误消息将我推向这个方向。

相关问题