jpa配置多对多关系(中间表)

两个实体tb_user,tb_role
现在我们再tb_user或者tb_role中任意一个里面进行维护关系,多对对的情况下我们需要创建一个中间表来完成这个关系的映射,我们再tb_user中添加注解@ManyToMany然后再添加一个注解@JoinTable因为我们是要创建中间表所以要使用这个注解。JoinTable注解中我们添加如下例子中的内容,joinColumns当前表中的字段在中间表中的字段名称,inverseJoinColumns关联的外键表在中间表中的字段名称


@Entity
@Table(name = "tb_user")
@SequenceGenerator(name = "tb_user_sq",sequenceName = "tb_user_sqe")
public class TbUser extends BaseEntity{


    /**
     * 用户名
     */
    private String userName;
    /**
     * 登录名
     */
    private String loginName;
    /**
     * 登陆密码
     */
    private String passWord;
    /**
     * 手机号
     */
    private String telPhone;
    /**
     * 一个用户可以有多个角色
     */
    private List tbRoleList=new ArrayList<>();

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getTelPhone() {
        return telPhone;
    }

    public void setTelPhone(String telPhone) {
        this.telPhone = telPhone;
    }

    @Id
    @Override
    @GeneratedValue(generator = "tb_user_sq",strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return this.id;
    }
    @ManyToMany(cascade = CascadeType.REMOVE,fetch = FetchType.LAZY)
    @JoinTable(name = "tb_user_role",joinColumns = @JoinColumn(name="tb_user_id",referencedColumnName = "id"),inverseJoinColumns = @JoinColumn(name = "tb_role_id",referencedColumnName = "id"))
    public List getTbRoleList() {
        return tbRoleList;
    }

    public void setTbRoleList(List tbRoleList) {
        this.tbRoleList = tbRoleList;
    }
}

因为在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 tbUserList=new ArrayList<>();

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    @ManyToMany(mappedBy = "tbRoleList")
    public List getTbUserList() {
        return tbUserList;
    }

    public void setTbUserList(List tbUserList) {
        this.tbUserList = tbUserList;
    }
}

你可能感兴趣的:(jpa)