JPA 启动自动 UUID 的 ID 生成策略

Felix 2019年05月16日 700次浏览

首先声明一个 uuid 策略,然后在需要的字段上声明ID生成方式。 之后在调用save方式的时候,就不需要为id赋值了。

@Data
@Entity
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class Client {
    @Id
    @GeneratedValue(generator = "jpa-uuid")
    private String id;
    private String userId;
    private int type; // 1. Monitor  2. PC
    private int status;  // 1. active  2. down
    private String channelId;
}