两个实体,一个是角色,一个是权限
对应三张表(角色表,中间表,权限表)
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,之前配置了这个,可能还有其它原因,导致了无法级联保存