首页 > 解决方案 > Spring JPA:主键是外键 - 此类未定义 IdClass

问题描述

我正在创建一个主键同时也是外键的实体。但是,我收到以下错误“此类未定义 IdClass”。可能是什么问题呢?

类报价

@Entity
@Data
@IdClass(Instrument.class)
public class Quote implements Serializable  {

    @Id
    @OneToOne
    @JoinColumn(name = "instrument")
    @NotNull
    private Instrument instrument;

    @NotNull
    private String time;

    @NotNull
    private double bid;

    @NotNull
    private double ask;

    @CreatedDate
    @Temporal(TIMESTAMP)
    @NotNull
    protected Date creationDate = new Date();

}

类仪器

@Entity
@Data
public class Instrument implements Serializable {

    @Id
    private String instrument;

    @NotNull
    private Currency currencyPrimary;

    @NotNull
    private Currency currencySecondary;
}

标签: springjpaprimary-key

解决方案


您缺少构造函数包含字段主键 @id.(instrumentId :这是我的示例),并且在 Instrument 类中缺少 equal 和 hashCode。


推荐阅读