求助,jpa为含有复合主键如何建立一对多的关联
1个回答
展开全部
进行面向对象的分析,uer和role之间是双向多对多联系,使用JPA配置如下所示:
@Entity
public class User implements Serizlizable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String userName;
private String passWord;
@ManyToMany(cascade = CascadeType.ALL)
private List<Role> roles;
......
......
}
@Entity
public class Role implements Serizlizable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String role;
@ManyToMany(mappedBy = "roles")
private User user;
......
......
}
配置后,会在数据库中生存你想要的3个表。
@Entity
public class User implements Serizlizable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String userName;
private String passWord;
@ManyToMany(cascade = CascadeType.ALL)
private List<Role> roles;
......
......
}
@Entity
public class Role implements Serizlizable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String role;
@ManyToMany(mappedBy = "roles")
private User user;
......
......
}
配置后,会在数据库中生存你想要的3个表。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询