1. 程式人生 > >Spring JPA 複合主鍵

Spring JPA 複合主鍵

Spring JPA 複合主鍵配置

1. 建立複合主鍵類,類上新增@Embeddable註解,注意要實現 Serializable 介面

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;

@Data
@Embeddable
public class PriKey implements Serializable {

    private String icId;

    private String orgId;

}

2. 實體類上使用複合主鍵類最為主鍵屬性,屬性上新增@EmbeddedId註解

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Data
@Entity
@Table(name = "tbl_ic_study_task")
public class IcStudy {

    @EmbeddedId
    private PriKey priKey;

    private String type;

}