Hibernate ManyToMany 配置

两个实体,一个是角色,一个是权限

对应三张表(角色表,中间表,权限表)

public class Authority implements java.io.Serializable {

    // Fields

    private Integer authorityid;
    private String authorityName;
    private String url;
    private Integer parentid;
    private List<Role> rlist;

    // Constructors

    /** default constructor */
    public Authority() {
    }

    /** full constructor */
    public Authority(String authorityName, String url, Integer parentid) {
        this.authorityName = authorityName;
        this.url = url;
        this.parentid = parentid;
    }

    // Property accessors
    @Id
    @GeneratedValue
    @Column(name = "authorityid", unique = true, nullable = false)
    public Integer getAuthorityid() {
        return this.authorityid;
    }

    public void setAuthorityid(Integer authorityid) {
        this.authorityid = authorityid;
    }

    @Column(name = "authorityName", length = 20)
    public String getAuthorityName() {
        return this.authorityName;
    }

    public void setAuthorityName(String authorityName) {
        this.authorityName = authorityName;
    }

    @Column(name = "url", length = 50)
    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Column(name = "parentid")
    public Integer getParentid() {
        return this.parentid;
    }

    public void setParentid(Integer parentid) {
        this.parentid = parentid;
    }
    @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinTable(name="Role_Authority",joinColumns={@JoinColumn(name="authorityid",referencedColumnName="authorityid")},inverseJoinColumns={@JoinColumn(name="roleid",referencedColumnName="roleid")})
    public List<Role> getRlist() {
        return rlist;
    }

    public void setRlist(List<Role> rlist) {
        this.rlist = rlist;
    }

}
public class Role implements java.io.Serializable {

    // Fields

    private Integer roleid;
    private String rolename;
    private List<User> users;
    private List<Authority> alist;

    // Constructors

    /** default constructor */
    public Role() {
    }

    /** full constructor */
    public Role(String rolename) {
        this.rolename = rolename;
    }

    // Property accessors
    @Id
    @GeneratedValue
    @Column(name = "roleid", unique = true, nullable = false)
    public Integer getRoleid() {
        return this.roleid;
    }

    public void setRoleid(Integer roleid) {
        this.roleid = roleid;
    }

    @Column(name = "rolename", length = 20)
    public String getRolename() {
        return this.rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }
    @OneToMany(mappedBy="role")
    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
    @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinTable(inverseJoinColumns={@JoinColumn(name="authorityid",referencedColumnName="authorityid")},
            joinColumns={@JoinColumn(name="roleid",referencedColumnName="roleid")},
            name="Role_Authority"
            )
    public List<Authority> getAlist() {
        return alist;
    }

    public void setAlist(List<Authority> alist) {
        this.alist = alist;
    }

}

ManyToMany不需要配置mapperBy,TargetEntity,之前配置了这个,可能还有其它原因,导致了无法级联保存

转载于:https://my.oschina.net/eviltuzki/blog/470593

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值