JPA 一对多单向 映射至Mysql的问题

matrix1984 2010-03-16 02:43:29
角色Role与权限Authority,我设计成一对多单向关联,数据库mysql:

//Role.java:

@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) {
...
}
}


//Authority.java:

@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) {
...
}

}


生成的中间表为role_auth,查看建表语句:

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)


如上,roleid具有唯一性,这样我在插入角色时就会出现类似“Duplicate entry '2' for key 2”的错误。
该怎么改呢??
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
matrix1984 2010-03-16
  • 打赏
  • 举报
回复
没人吗?自己顶下咯。

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧