1. The use of spring data jpa
The following two articles are recommended
Using spring data jpa using
spring-boot-jpa
Second, set multiple primary keys (composite primary keys)
Two methods are described below:
The first: @IdClass to set multiple primary keys
1. First write a class that contains the primary key
@Data
public class PrimaryKey implements Serializable {
private Integer id;
private Integer userId;
}
2. In the Entity class, use it as follows
@Data
@Entity
@Table(name = "xx")
@IdClass(PrimaryKey.class)
@EqualsAndHashCode(callSuper = true)
@DynamicUpdate
public class MyEntity{
/**
* @description 主键
*/
@Id
@Column(name = "id", nullable = false)
private Integer id;
/**
* @description 主键
*/
@Id
@Column(name = "user_id", nullable = false)
private Integer userId;
}
refer to:
Spring Boot JPA composite primary key only queries part of the primary key
The second: @Embeddable and @EmbeddedId to set the joint primary key
Recommend this article: Two properties define a primary key: Union primary key in JPA