JPA – 带有@ManytoOne的EmbeddedId
我的代码有问题(很明显),经过互联网上的多次搜索,我找不到问题的答案,所以我在这里问我的问题。 我有这个 :
@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中写了这个:
beans.Port beans.Resident
但是当我运行程序时,无论我写什么,我都有:
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对象,但我不知道怎么把它弄错
您得到的错误消息很好地解释了它,用作嵌入式ID的Embeddable只能包含基本映射,而不能包含关系。 在JPA 2.0规范中,这用以下词语告知:
不支持在嵌入式id类中定义的关系映射。
只需定义属于embeddable中复合id的一部分的属性,用作嵌入式id,并在实体本身中映射关系(或者在另一个可嵌入和包含@Embedded的映射中)。
在我看来,这是基于IdResident类中的ManyToOne映射导致错误消息将我推向这个方向。