复合主键的使用@IdClass

复合主键主要是指你的数据表中有两个主键

1、创建复合主键类

@Data
public class SourceTimeTagPK implements Serializable {

    @Basic
    @Column(name = "source_time")
    private Timestamp source_time;

    @Basic
    @Column(name = "tag")
    private String tag;

}

2、在实体类中

@Entity
@Table(name = "metrics")
@Data
@IdClass(SourceTimeTagPK.class)
public class MetricsEntity implements Serializable {

    @Id
    @Column(name = "source_time",nullable = false)
    private Timestamp sourceTime;

    @Id
    @Column(name = "tag",nullable = false)
    private String tag;

    @Basic
    @Column(name = "value")
    private double value;

    @Basic
    @Column(name = "quality")
    private short quality;

    //构造函数
    public MetricsEntity(Timestamp sourceTime,String tag, double value, short quality) {
        this.sourceTime = sourceTime;
        this.tag = tag;
        this.value = value;
        this.quality = quality;

    }
}

猜你喜欢

转载自blog.csdn.net/qq_28289405/article/details/81020293