用戶、角色、權限三者多對多用hibernate的一對多注解配置
//權限表
@Table(name = "p")
public class P {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "p_id", unique = true, nullable = false)
private Integer id;
@OneToMany(mappedBy = "p",cascade=CascadeType.ALL)
private Set<PR> pr = new HashSet<PR>();
/*省略所有get、set方法及其他列*/
}
//權限角色表
@Entity
@Table(name = "pr")
public class PR {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pr_id", unique = true, nullable = false)
private Integer id;
@ManyToOne
@JoinColumn(name = "r_id")
private R r;
@ManyToOne
@JoinColumn(name = "p_id")
private P p;
/*省略所有get、set方法及其他列*/
}
//角色表
@Entity
@Table(name = "r")
public class R {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "r_id", unique = true, nullable = false)
private Integer id;
@OneToMany(mappedBy = "r",cascade=CascadeType.ALL)
private Set<PR> pr = new HashSet<PR>();
@OneToMany(mappedBy = "r",cascade=CascadeType.ALL)
private Set<RU> ru = new HashSet<RU>();
/*省略所有get、set方法及其他列*/
}
//角色用戶關系表
@Entity
@Table(name = "ru")
public class RU {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ru_id", unique = true, nullable = false)
private Integer id;
@ManyToOne
@JoinColumn(name = "r_id")
private R r;
@ManyToOne
@JoinColumn(name = "u_id")
private U u;
/*省略所有get、set方法及其他列*/
}
//用戶表
@Entity
@Table(name = "u")
public class U {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "u_id", unique = true, nullable = false)
private Integer id;
@OneToMany(mappedBy = "u",cascade=CascadeType.ALL)
private Set<RU> ru = new HashSet<RU>();
/*省略所有get、set方法及其他列*/
}
數據庫圖片示例:
結果如上。(P《權限表》、R《角色表》、U《用戶表》、PR《權限角色關系表》、RU《角色用戶關系表》)