archy123 2010-06-11 14:17
浏览 230
已采纳

hibernate 实现用户角色对应关系的问题

[size=small][color=olive]简单的权限管理表设计
不知道这样的表间对应关系是否正确,包含用户,角色
以及中间表 ,还请指教。[/color][/size]
PubUser(用户表)
简化代码
[code="java"]
{
private Integer userId;
private Set pubUsersRoleses = new HashSet(0);

@Id
@GeneratedValue
@Column(name = "user_id", unique = true, nullable = false)
public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pubUsers")
public Set<PubUsersRoles> getPubUsersRoleses() {
    return this.pubUsersRoleses;
}

public void setPubUsersRoleses(Set<PubUsersRoles> pubUsersRoleses) {
    this.pubUsersRoleses = pubUsersRoleses;
}

}
[/code]
pubRoles(角色表)
[code="java"]

@Entity
public class PubRoles implements java.io.Serializable {

private Integer roleId;

private Set<PubUsersRoles> pubUsersRoleses = new HashSet<PubUsersRoles>(0);


@Id
@GeneratedValue
@Column(name = "role_id", unique = true, nullable = false)
public Integer getRoleId() {
    return this.roleId;
}

public void setRoleId(Integer roleId) {
    this.roleId = roleId;
}



@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pubRoles")
public Set<PubUsersRoles> getPubUsersRoleses() {
    return this.pubUsersRoleses;
}

public void setPubUsersRoleses(Set<PubUsersRoles> pubUsersRoleses) {
    this.pubUsersRoleses = pubUsersRoleses;
}

}
[/code]
pubUsersRoles (用户角色中间表)
[code="java"]

public class PubUsersRoles implements java.io.Serializable {

private Long id;
private PubUsers pubUsers;
private PubRoles pubRoles;


@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false, precision = 12, scale = 0)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
public PubUsers getPubUsers() {
    return this.pubUsers;
}

public void setPubUsers(PubUsers pubUsers) {
    this.pubUsers = pubUsers;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
public PubRoles getPubRoles() {
    return this.pubRoles;
}

public void setPubRoles(PubRoles pubRoles) {
    this.pubRoles = pubRoles;
}

}
[/code]

  • 写回答

1条回答 默认 最新

  • iteye_14351 2010-06-11 14:28
    关注

    正确。角色和权限再对应关系就好了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算