[TOC]

jpa多主键

方案一 @IdClass来设置多个主键

1、先写一个包含主键的类。 需要实现 Serializable 接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 用户应用权限主键
* @author xia17
* @date 2021/9/7
*/
@Getter
@Setter
@EqualsAndHashCode
public class UserAppKey implements Serializable {

private Long userId;

private Long appId;


}

2、在Entity类中,按照如下方式使用。需要实现 Serializable 接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Getter
@Setter
@EqualsAndHashCode
@Entity
@Table(name = "cas_user_app")
@Accessors(chain = true)
@IdClass(UserAppKey.class)
public class UserApp implements Serializable {

@Id
private Long userId;

@Id
private Long appId;


}

方案二 @Embeddable和@EmbeddedId来设置联合主键

暂无