package com.cserver.core.domain;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name = "SYS_MENU")
@SuppressWarnings("serial")
public class SysMenu implements java.io.Serializable {
private Long oid;
/**
* 父类id
*/
private SysMenu parentMenu;
/**
*子类id
*/
private List<SysMenu> childMenu;
/**
* 菜单名ch
*/
@Column(name = "MENUNAME", length = 40)
private String menuName;
/**
* 菜单名
*/
@Column(name = "MENUENNAME", length = 36)
private String menuEnName;
/**
* 角色
*/
private Set<SysRole> sysRoles;
/**
* 菜单连接
*/
@Column(name = "MENUURL", length = 255)
private String menuUrl;
/***
* 排序字段
*/
@Column(name = "SORTNUM", precision = 4, scale = 0)
private Long sortnum;
/**
* 级别
*/
@Column(name="menuLevel", precision = 4, scale = 0)
private Long menuLevel;
/**
* 添加人
*/
@Column(name="usercode", length = 40)
private String usercode;
/**
* 创建时间
*/
@Column(name="createDate", length = 40)
private String createDate;
/**
* 描述
*/
@Column(name = "menuDescribe", length = 200)
private String menuDescribe;
/**
* 0:无效
* 1:有效
*/
@Column(name="stuats", length = 4)
private String stuats;
public SysMenu() {}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SM_SYS_SEQ")
@SequenceGenerator(name = "SM_SYS_SEQ", sequenceName = "SM_SYS_SEQ", allocationSize = 1)
@Column(name = "OID", nullable = false, precision = 12, scale = 0)
public Long getOid() {
return this.oid;
}
public void setOid(Long oid) {
this.oid = oid;
}
/**
* PERSIST 级联添加
* MERGE 级联修改
* REFRESH
* REMOVE 级联删除
* @return
*/
@ManyToOne(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "parent_oid")
@NotFound(action=NotFoundAction.IGNORE)
public SysMenu getParentMenu() {
return parentMenu;
}
public void setParentMenu(SysMenu parentMenu) {
this.parentMenu = parentMenu;
}
@OneToMany(mappedBy="parentMenu", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
public List<SysMenu> getChildMenu() {
return childMenu;
}
public void setChildMenu(List<SysMenu> childMenu) {
this.childMenu = childMenu;
}
/**
* @ManyToMany 注释表示Student是多对多关系的一边
* mappedBy 属性定义了SysMenu 为双向关系的维护端
*/
@ManyToMany(mappedBy="menus", cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},fetch=FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
public Set<SysRole> getSysRoles() {
return sysRoles;
}
public void setSysRoles( Set<SysRole> sysRoles) {
this.sysRoles = sysRoles;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public String getMenuEnName() {
return menuEnName;
}
public void setMenuEnName(String menuEnName) {
this.menuEnName = menuEnName;
}
public String getMenuUrl() {
return menuUrl;
}
public void setMenuUrl(String menuUrl) {
this.menuUrl = menuUrl;
}
public Long getSortnum() {
return sortnum;
}
public void setSortnum(Long sortnum) {
this.sortnum = sortnum;
}
public Long getMenuLevel() {
return menuLevel;
}
public void setMenuLevel(Long menuLevel) {
this.menuLevel = menuLevel;
}
public String getUsercode() {
return usercode;
}
public void setUsercode(String usercode) {
this.usercode = usercode;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getStuats() {
return stuats;
}
public void setStuats(String stuats) {
this.stuats = stuats;
}
public String getMenuDescribe() {
return menuDescribe;
}
public void setMenuDescribe(String menuDescribe) {
this.menuDescribe = menuDescribe;
}
}
package com.cserver.core.domain;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name = "SYS_ROLE")
@SuppressWarnings("serial")
public class SysRole implements java.io.Serializable{
private Long oid;
/** 角色名称 */
private String roleName;
/**
* 0:未启用
* 1:启用
*/
private String stuats;
/** 添加人 */
private String usercode;
/** 创建时间 */
private String createDate;
/** 描述 */
private String describe;
/** 特殊角色标识 */
private String constants;
/** 菜单 */
private Set<SysMenu> menus;
/** 用户 */
private Set<SysUser> sysUsers;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SM_SYS_SEQ")
@SequenceGenerator(name = "SM_SYS_SEQ", sequenceName = "SM_SYS_SEQ", allocationSize = 1)
@Column(name = "OID", nullable = false, precision = 22, scale = 0)
public Long getOid() {
return this.oid;
}
public void setOid(Long oid) {
this.oid = oid;
}
/** 特殊角色标识 */
@Column(name="constants", length = 200)
public String getConstants() {
return constants;
}
/** 特殊角色标识 */
public void setConstants(String constants) {
this.constants = constants;
}
/** 添加人 */
@Column(name="usercode", length = 20)
public String getUsercode() {
return usercode;
}
/** 添加人 */
public void setUsercode(String usercode) {
this.usercode = usercode;
}
/** 创建时间 */
@Column(name="createDate", length = 40)
public String getCreateDate() {
return createDate;
}
/** 创建时间 */
public void setCreateDate(String createDate) {
this.createDate =createDate;
}
/**
* 0:未启用
* 1:启用
*/
@Column(name="stuats", length = 40)
public String getStuats() {
return stuats;
}
/**
* 0:未启用
* 1:启用
*/
public void setStuats(String stuats) {
this.stuats = stuats;
}
/*
* @ManyToMany 注释表示SysRole 是多对多关系的一端。
* @JoinTable 描述了多对多关系的数据表关系,name属性指定中间表名称。
* joinColumns 定义中间表与SysRole 表的外键关系,中间表SYS_ROLE_MENU的roleOid 列是SysRole 表的主键列对应的外键列。
* inverseJoinColumns 属性定义了中间表与另外一端(SysMenu)的外键关系。
*/
@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name="SYS_ROLE_MENU",joinColumns={@JoinColumn(name="roleOid",referencedColumnName="oid")},
inverseJoinColumns={@JoinColumn(name="menuOid",referencedColumnName="oid")})
@NotFound(action=NotFoundAction.IGNORE)
public Set<SysMenu> getMenus() {
return menus;
}
public void setMenus(Set<SysMenu> menus) {
this.menus = menus;
}
/*
* @ManyToMany 注释表示SysRole 是多对多关系的一端。
* @JoinTable 描述了多对多关系的数据表关系,name属性指定中间表名称。
* joinColumns 定义中间表与SysRole 表的外键关系,中间表SYS_ROLE_USER的roleOid 列是SysRole 表的主键列对应的外键列。
* inverseJoinColumns 属性定义了中间表与另外一端(SysUser)的外键关系。
*/
@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name="SYS_ROLE_USER",joinColumns={@JoinColumn(name="roleOid",referencedColumnName="oid")},
inverseJoinColumns={@JoinColumn(name="userOid",referencedColumnName="oid")})
@NotFound(action=NotFoundAction.IGNORE)
public Set<SysUser> getSysUsers() {
return sysUsers;
}
public void setSysUsers( Set<SysUser> sysUsers) {
this.sysUsers = sysUsers;
}
@Column(name = "ROLENAME", nullable = false)
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@Column(name = "ROLEDESCRIBE", length = 300)
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
保存关联:
/**
* 关联菜单到角色
* @param sysRole
* @param menuOids
* @throws Exception
*/
public void linkRoleToMenus(SysRole sysRole,String menuOids)throws Exception{
sysRole = this.findRoleByOid(sysRole.getOid());
Set<SysMenu> menus = new HashSet<SysMenu>();
try {
if (StringUtils.isNotEmpty(menuOids)) {
String[] s = menuOids.split(",");
for (String oid : s) {
SysMenu menu = (SysMenu) genericDAO.getObject(SysMenu.class, Long.parseLong(oid));
if (menu != null) {
menus.add(menu);
}
}
}
sysRole.setMenus(menus);
} catch (Exception e) {
e.printStackTrace();
}
genericDAO.updateObject(sysRole);
}
注:在@ManyToMany的选项中mappedBy放置到另一方则两者关系保存不到中间表中