因为在tb_user中我们维护了两个表的关系,所以如果我们在tb_role中如果不想创建关联字段的话就不用添加tbUser的关系字段
@Entity @Table(name = "tb_role") @SequenceGenerator(name = "tb_role_sq",sequenceName = "tb_role_sqe") public class TbRole extends BaseEntity{ @Override @Id @GeneratedValue(generator = "tb_role_sq",strategy = GenerationType.SEQUENCE) public Long getId() { return this.id; } private String roleName; @ManyToMany(mappedBy = "tbRoleList") public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; }如果想要在tb_role中进行维护关联字段的话如下,我们在字段中添加注解 @ManyToMany然后使用直接属性mappedBy值是当前表在关联表中的字段名称
@Entity @Table(name = "tb_role") @SequenceGenerator(name = "tb_role_sq",sequenceName = "tb_role_sqe") public class TbRole extends BaseEntity{ @Override @Id @GeneratedValue(generator = "tb_role_sq",strategy = GenerationType.SEQUENCE) public Long getId() { return this.id; } private String roleName; private List<TbUser> tbUserList=new ArrayList<>(); public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } @ManyToMany(mappedBy = "tbRoleList") public List<TbUser> getTbUserList() { return tbUserList; } public void setTbUserList(List<TbUser> tbUserList) { this.tbUserList = tbUserList; }