Java JPA - 带有 @ManytoOne 的 EmbeddedId

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9705130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:27:19  来源:igfitidea点击:

JPA - EmbeddedId with @ManytoOne

javajpamaven

提问by Carvallegro

I have a problem with my code (obviously) and after many searches on Internet, I don't find an answer to my problem, so I ask my question here. I have this :

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

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

And I'm using Maven, I've write this in my persistence.xml :

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

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

But when i run the program, no matter what i've write, I have this :

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

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.

I don't see where is my mistake, I think it's because of the IdResident class wich has an Entity object in it, but I don't know how to fiw it

我不明白我的错误在哪里,我认为这是因为 IdResident 类中有一个实体对象,但我不知道如何解决

采纳答案by Mikko Maunu

Error message you get explains it quite well, Embeddable that is used as an embedded id can contain only basic mappings, not relationships. In JPA 2.0 specification this is told with following words:

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

Relationship mappings defined within an embedded id class are not supported.

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

Just define attributes that are part of composite id in embeddable that is used as embedded id, and map relationships in entity itself (or in another embeddable and include mappings with @Embedded).

只需在用作嵌入 id 的 embeddable 中定义作为复合 id 一部分的属性,并映射实体本身(或另一个可嵌入并包含与 @Embedded 的映射)的关系。

回答by khmarbaise

In my opinion this is based on the ManyToOne mapping in the IdResident class cause the error message pushs me into this direction.

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