Jpa之复合主键
程序员文章站
2022-04-24 22:33:05
...
jpa设置组合主键需要新建复合主键类,而且需要实现 Serializable接口
public class RolePermissionKey implements Serializable {
private int rid;
private String pid;
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public RolePermissionKey() {
}
public RolePermissionKey(int rid, String pid) {
this.rid = rid;
this.pid = pid;
}
}
在实体类
@Entity
@Table(name = "t_role_permission")
@IdClass(com.zking.p2.entity.RolePermissionKey.class) //指定你的组合主键类
public class RolePermission {
private int rid;
private String pid;
@Id
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
@Id
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
}