SpringData 完全入门指南 (5)

因为在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; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxzsd.html