【问题标题】:JPA - EmbeddedId with @ManytoOneJPA - EmbeddedId 与 @ManytoOne
【发布时间】:2012-03-31 02:22:51
【问题描述】:

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

@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 对象,但我不知道如何解决它

【问题讨论】:

    标签: java jpa maven


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2016-07-05
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 2019-03-31
        • 2011-11-03
        • 2011-05-04
        相关资源
        最近更新 更多