Eclipse IDE 显示 JPA 中复合主键类的错误

cal*_*.mk 4 java eclipse hibernate jpa composite-primary-key

我有一个带有复合 PK 的实体类,如下所示:
使用 @Embeddable 和 @EmbeddedId 注释。

/** The primary key class for the uom_conversion database table. */
@Embeddable
public class UomConversionPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="product_id", insertable=false, updatable=false)
    private int productId;

    @Column(name="base_uom_id", insertable=false, updatable=false)
    private int baseUomId;

    @Column(name="to_unit_id", insertable=false, updatable=false)
    private int toUnitId;
    //getters, setters
}
Run Code Online (Sandbox Code Playgroud)

使用它的实体是:

/** The persistent class for the uom_conversion database table. */
@Entity
@Table(name="uom_conversion")
public class UomConversion implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private UomConversionPK id;
}
Run Code Online (Sandbox Code Playgroud)

这里 Eclipse 显示错误:“UomConversionPK 无法解析为类型”

Eclipse 错误快照

在另一个项目中,我将实体与复合 PK 一起使用,没有任何错误。

现在这似乎是 JPA 方面的问题,想知道为什么 Eclipse 无法解析 UomConversionPK 或者我做错了什么?

小智 5

好吧@EmbeddedId,当使用 JPA 工具从表创建 JPA 实体时,有时会发生此错误以进行注释,但并非总是如此。

在我的情况下,直到我将以下内容设置为Ignore然后返回到Error:后,此问题才得到解决
Windows -> Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attribute -> Embedded ID classes should implement hashcode() and equals()。即使 Embeddable 类确实具有 hashcode() 和 equals() 方法实现,也会发生此错误。可能这也适用于您。