角色 与 菜单(资源)的多对多关系,在这里我们建立中间表,用两个oneToMany实现
实体类:
角色(GmRole) 菜单(GmMenu) 中间表(GmRoleRight)
1.在角色实体中
package com.bossy.gun.entity; import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name="gm_role") public class GmRole implements java.io.Serializable { private static final long serialVersionUID = 2740105939852999578L;
private String roleId;
private String roleName;
private String roleState;
private Set<GmRoleRight> gmRoleRights = new HashSet<GmRoleRight>(0); public GmRole() {
} public GmRole(String roleId) {
this.roleId = roleId;
} public GmRole(String roleId, String roleName, String roleState, Set<GmRoleRight> gmRoleRights) {
this.roleId = roleId;
this.roleName = roleName;
this.roleState = roleState;
this.gmRoleRights = gmRoleRights;
} @Id
@Column(name="role_id", unique=true, nullable=false, length=50)
public String getRoleId() {
return this.roleId;
} public void setRoleId(String roleId) {
this.roleId = roleId;
} @Column(name="role_name", length=100)
public String getRoleName() {
return this.roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} @Column(name="role_state", length=10)
public String getRoleState() {
return this.roleState;
} public void setRoleState(String roleState) {
this.roleState = roleState;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="gmRole")
public Set<GmRoleRight> getGmRoleRights() {
return this.gmRoleRights;
} public void setGmRoleRights(Set<GmRoleRight> gmRoleRights) {
this.gmRoleRights = gmRoleRights;
} @Override
public String toString() {
return "GmRole [roleId=" + roleId + ", roleName=" + roleName
+ ", roleState=" + roleState + ", gmRoleRights=" + gmRoleRights
+ "]";
}
}
2.菜单实体中
package com.bossy.gun.entity; import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name="gm_menu") public class GmMenu implements java.io.Serializable { private static final long serialVersionUID = -4791621393144183524L;
private String menuId;
private String menuFaId;
private String menuName;
private String menuIoc;
private String menuLink;
private String menuSort;
private String menuState;
private String menuType;
private String menuCkname;
private Set<GmRoleRight> gmRoleRights = new HashSet<GmRoleRight>(0); public GmMenu() {
} public GmMenu(String menuId) {
this.menuId = menuId;
} public GmMenu(String menuId, String menuFaId, String menuName, String menuIoc, String menuLink, String menuSort, String menuState,String menuType,String menuCkname, Set<GmRoleRight> gmRoleRights) {
this.menuId = menuId;
this.menuFaId = menuFaId;
this.menuName = menuName;
this.menuIoc = menuIoc;
this.menuLink = menuLink;
this.menuSort = menuSort;
this.menuState = menuState;
this.gmRoleRights = gmRoleRights;
this.menuType = menuType;
this.menuCkname = menuCkname;
} @Id
@Column(name="menu_id", unique=true, nullable=false, length=50) public String getMenuId() {
return this.menuId;
} public void setMenuId(String menuId) {
this.menuId = menuId;
} @Column(name="menu_fa_id", length=50)
public String getMenuFaId() {
return this.menuFaId;
} public void setMenuFaId(String menuFaId) {
this.menuFaId = menuFaId;
} @Column(name="menu_name", length=100)
public String getMenuName() {
return this.menuName;
} public void setMenuName(String menuName) {
this.menuName = menuName;
} @Column(name="menu_ioc", length=200)
public String getMenuIoc() {
return this.menuIoc;
} public void setMenuIoc(String menuIoc) {
this.menuIoc = menuIoc;
} @Column(name="menu_link", length=400)
public String getMenuLink() {
return this.menuLink;
} public void setMenuLink(String menuLink) {
this.menuLink = menuLink;
} @Column(name="menu_sort", length=10)
public String getMenuSort() {
return this.menuSort;
} public void setMenuSort(String menuSort) {
this.menuSort = menuSort;
} @Column(name="menu_state", length=10)
public String getMenuState() {
return this.menuState;
} public void setMenuState(String menuState) {
this.menuState = menuState;
} @Column(name="menu_type", length=10)
public String getMenuType() {
return menuType;
} public void setMenuType(String menuType) {
this.menuType = menuType;
} @Column(name="menu_ckname", length=10)
public String getMenuCkname() {
return menuCkname;
} public void setMenuCkname(String menuCkname) {
this.menuCkname = menuCkname;
} @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="gmMenu")
public Set<GmRoleRight> getGmRoleRights() {
return gmRoleRights;
}
public void setGmRoleRights(Set<GmRoleRight> gmRoleRights) {
this.gmRoleRights = gmRoleRights;
} }
3.中间表中
package com.bossy.gun.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table; @Entity
@Table(name = "gm_role_right")
public class GmRoleRight implements java.io.Serializable { private static final long serialVersionUID = -1245261414803172237L;
private String grrId;
private String grrState;
private GmRole gmRole;
private GmMenu gmMenu; public GmRoleRight() {
} public GmRoleRight(String grrId) {
this.grrId = grrId;
} public GmRoleRight(String grrId, String grrState, GmRole gmRole,
GmMenu gmMenu) {
super();
this.grrId = grrId;
this.grrState = grrState;
this.gmRole = gmRole;
this.gmMenu = gmMenu;
} @Id
@Column(name = "grr_id", unique = true, nullable = false, length = 50)
public String getGrrId() {
return this.grrId;
}
public void setGrrId(String grrId) {
this.grrId = grrId;
} @Column(name = "grr_state", length = 50)
public String getGrrState() {
return this.grrState;
}
public void setGrrState(String grrState) {
this.grrState = grrState;
} @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="role_id")
public GmRole getGmRole() {
return this.gmRole;
} public void setGmRole(GmRole gmRole) {
this.gmRole = gmRole;
} @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="menu_id")
public GmMenu getGmMenu() {
return gmMenu;
}
public void setGmMenu(GmMenu gmMenu) {
this.gmMenu = gmMenu;
}
@Override
public String toString() {
return "GmRoleRight [grrId=" + grrId + ", grrState=" + grrState
+ ", gmRole=" + gmRole + ", gmMenu=" + gmMenu + "]";
}
}