81,114
社区成员
发帖
与我相关
我的任务
分享
@Entity
public class Role implements Serializable {
private Integer roleid;
private String rolename;
private String description;
private Set<Authority> auths = new HashSet<Authority>();
@Column
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Id
@GeneratedValue
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
@Column
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
//一对多单向关联
@OneToMany(cascade = CascadeType.REFRESH, fetch=FetchType.EAGER)
@JoinTable(name = "role_auth", joinColumns = @JoinColumn(name = "roleid"),
inverseJoinColumns = @JoinColumn(name = "authorityid"))
public Set<Authority> getAuths() {
return auths;
}
public void setAuths(Set<Authority> auths) {
this.auths = auths;
}
// 添加权限
public void addAuth(Authority auth) {
this.auths.add(auth);
}
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object obj) {
...
}
}
@Entity
public class Authority implements Serializable {
private Integer authorityid;
// 范围
private Range range;
// 动作
private Action action;
public Authority() {}
public Authority(Range range, Action action) {
this.range = range;
this.action = action;
}
@ManyToOne
@JoinColumn(name="actionid",
referencedColumnName="actionid")
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
@Id
@GeneratedValue
public Integer getAuthorityid() {
return authorityid;
}
public void setAuthorityid(Integer authorityid) {
this.authorityid = authorityid;
}
@ManyToOne
@JoinColumn(name="rangeid",
referencedColumnName="rangeid")
public Range getRange() {
return range;
}
public void setRange(Range range) {
this.range = range;
}
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object obj) {
...
}
}
mysql> show create table role_auth;
| Table | Create Table
| role_auth | CREATE TABLE `role_auth` (
`roleid` int(11) NOT NULL,
`authorityid` int(11) NOT NULL,
PRIMARY KEY (`roleid`,`authorityid`),
UNIQUE KEY `authorityid` (`authorityid`), //这里roleid被定义成唯一性?!
KEY `FK13FEEFD183497181` (`roleid`),
KEY `FK13FEEFD1270B3A27` (`authorityid`),
CONSTRAINT `FK13FEEFD1270B3A27` FOREIGN KEY (`authorityid`) REFERENCES `author
ity` (`authorityid`),
CONSTRAINT `FK13FEEFD183497181` FOREIGN KEY (`roleid`) REFERENCES `role` (`rol
eid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)