JPA-带@ManytoOne的EmbeddedId [英] JPA - EmbeddedId with @ManytoOne

查看:119
本文介绍了JPA-带@ManytoOne的EmbeddedId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题(很明显),并且在Internet上进行了许多搜索之后,我找不到我的问题的答案,因此我在这里提出我的问题. 我有这个:

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

我正在使用Maven,已将其写在persistence.xml中:

And I'm using Maven, I've write this in my 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.

我看不出我的错误在哪里,我认为这是因为IdResident类中有一个Entity对象,但我不知道该怎么办

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

推荐答案

您收到的错误消息解释得很好,用作嵌入式ID的Embeddable只能包含基本映射,而不能包含关系.在JPA 2.0规范中,这是用以下单词来表示的:

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类中定义的关系映射不是 支持.

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

只需定义作为可嵌入ID的可嵌入对象中的复合ID的一部分的属性,并在实体本身(或在另一个可嵌入对象中并包含具有@Embedded的映射)中映射关系.

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).

这篇关于JPA-带@ManytoOne的EmbeddedId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆