一对多和多对多
一个组织拥有多个用户,一个组织有多个角色,一个角色有多个组织
package com.eshore.ismp.permission.model; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; 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.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; @Entity @SuppressWarnings("serial") @Table(name="SYS_ORGANIZATION") public class SysOrganization implements Serializable{ private Long id; private String nodeId; private int state; private String name; private String description; private String phone; private String email; private Date createTime; private Date updateTime; private List<Long> roleIds = new ArrayList<Long>(); private Set<SysRole> roles = new HashSet<SysRole>(0); private Set<SysUserB> users = new HashSet<SysUserB>(0); @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false, length = 32) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="node_id") public String getNodeId() { return nodeId; } public void setNodeId(String nodeId) { this.nodeId = nodeId; } @Column(name="state") public int getState() { return state; } public void setState(int state) { this.state = state; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="description") public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Column(name="phone") public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name="email") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name="create_time") public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Column(name="update_time") public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_ORGANIZATION_ROLE", schema = "", joinColumns = { @JoinColumn(name = "organization_id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }) public Set<SysRole> getRoles() { return roles; } public void setRoles(Set<SysRole> roles) { this.roles = roles; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "organization", cascade = CascadeType.ALL) public Set<SysUserB> getUsers() { return users; } public void setUsers(Set<SysUserB> users) { this.users = users; } @Transient public List<Long> getRoleIds() { return roleIds; } public void setRoleIds(List<Long> roleIds) { this.roleIds = roleIds; } }
USER
package com.model; import java.io.Serializable; import java.util.Date; import java.util.HashSet; import java.util.Set; 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.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; @Entity @SuppressWarnings("serial") @Table(name="SYS_USER") public class SysUser implements Serializable{ private Long id; //ID private String loginName; //登录名 private String name; //名称 private String password; //密码 private Long nodeId;//节点 private SysOrganization organization; // 所属组织 private String phone; //电话 private String mobilePhone; //手机 private String email; //邮箱 private String description; //描述 private String creatorId; //创建人 private Date createTime; // 创建时间 private Date updateTime; //更新时间 private Set<String> roleIds; // 角色ID private int type; // 类型 0 ismp-b ,1 ismp-hb,2 ismp-yh private String roleIdS; //角色ID private String orgName; //组织名 private String nodeName; //节点名 private Set<SysRole> roles = new HashSet<SysRole>(0); // 角色ID private Long orgId;//组织ID @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false, length = 32) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="type") public int getType() { return type; } public void setType(int type) { this.type = type; } @Column(name = "description", unique = false, nullable = true, insertable = true, updatable = true, length = 255) public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Column(name = "node_id", unique = false, nullable = true, insertable = true, updatable = true) public Long getNodeId() { return nodeId; } public void setNodeId(Long nodeId) { this.nodeId = nodeId; } @Column(name = "create_id", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getCreatorId() { return creatorId; } public void setCreatorId(String creatorId) { this.creatorId = creatorId; } @Column(name = "password", unique = false, nullable = true, insertable = true, updatable = true, length = 255) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "organization_id") public SysOrganization getOrganization() { return organization; } public void setOrganization(SysOrganization organization) { this.organization = organization; } @Column(name = "phone", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name = "mobile_phone", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getMobilePhone() { return mobilePhone; } public void setMobilePhone(String mobilePhone) { this.mobilePhone = mobilePhone; } @Column(name = "email", unique = false, nullable = true, insertable = true, updatable = true, length = 60) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name = "login_name", unique = false, nullable = true, insertable = true, updatable = true) public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @Column(name = "create_time", unique = false, nullable = true, insertable = true, updatable = true) public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Column(name = "update_time", unique = false, nullable = true, insertable = true, updatable = true) public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Transient public Set<String> getRoleIds() { return roleIds; } public void setRoleIds(Set<String> roleIds) { this.roleIds = roleIds; } @Transient public String getRoleIdS() { return roleIdS; } public void setRoleIdS(String roleIdS) { this.roleIdS = roleIdS; } @Transient public String getOrgName() { if(organization!=null){ return organization.getName(); } return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } @Transient public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_USER_ROLE", schema = "", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }) public Set<SysRole> getRoles() { return roles; } public void setRoles(Set<SysRole> roles) { this.roles = roles; } @Transient public Long getOrgId() { return orgId; } public void setOrgId(Long orgId) { this.orgId = orgId; } @Override public String toString() { return "SysUser [loginName=" + loginName + ", createTime=" + createTime + "]"; } }
SYSROLE
package com.model; import java.io.Serializable; import java.util.Date; 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.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; @Entity @SuppressWarnings("serial") @Table(name="SYS_ROLE") public class SysRole implements Serializable{ private Long id; //ID private String name; //名称 private int type;//类型 0 -B, 1-HB,2-YH private String createrName;//创建人 private String description; //描述 private Date createTime; //创建时间 private Date updateTime; //更新时间 private Long[] resourceIds; private Set<SysUserRole> userRoles = new HashSet<SysUserRole>(0); // private Set<SysUser> users = new HashSet<SysUser>(0); //关联用户 // private Set<SysOrganization> organizations = new HashSet<SysOrganization>(0); //管理组织 private Set<SysResource> resources = new HashSet<SysResource>(0); //管理权限 @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false, length = 32) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="type") public int getType() { return type; } public void setType(int type) { this.type = type; } @Column(name="creater_name") public String getCreaterName() { return createrName; } public void setCreaterName(String createrName) { this.createrName = createrName; } @Column(name="description") public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Column(name="create_time") public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Column(name="update_time") public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } /* @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_USER_ROLE", schema = "", joinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }) public Set<SysUser> getUsers() { return users; } public void setUsers(Set<SysUser> users) { this.users = users; } */ @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="role_id",updatable = false) public Set<SysUserRole> getUserRoles() { return userRoles; } public void setUserRoles(Set<SysUserRole> userRoles) { this.userRoles = userRoles; } // // @ManyToMany(mappedBy="roles",cascade = CascadeType.MERGE,fetch = FetchType.LAZY) // public Set<SysOrganization> getOrganizations() { // return organizations; // } // public void setOrganizations(Set<SysOrganization> organizations) { // this.organizations = organizations; // } // @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_ROLE_RESOURCE", schema = "", joinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "resource_id", nullable = false, updatable = false) }) public Set<SysResource> getResources() { return resources; } public void setResources(Set<SysResource> resources) { this.resources = resources; } @Transient public Long[] getResourceIds() { return resourceIds; } public void setResourceIds(Long[] resourceIds) { this.resourceIds = resourceIds; } }
SYSUSERB
package com.eshore.ismp.permission.model; import java.util.Date; import java.util.HashSet; import java.util.Set; 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.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; @Entity @SuppressWarnings("serial") @Table(name="SYS_USER_B") public class SysUserB { private Long id; //ID private String loginName; //登录名 private String password; //密码 private String name; //姓名 private String nodeId;//节点 private SysOrganization organization; // 所属组织 private String phone; //电话 private String mobilePhone; //手机 private String email; //邮箱 private String description; //描述 private int type; // 类型 0 ismp-b ,1 ismp-hb,2 ismp-yh private String creatorId; //创建人 private Date createTime; // 创建时间 private Date updateTime; //更新时间 private Set<String> roleIds; // 角色ID private String roleIdS; //角色ID private String orgName; //组织名 private String nodeName; //节点名 private Set<SysRole> roles = new HashSet<SysRole>(0); // 角色ID private Integer adminType; //0-非管理员,1-管理员 private Long orgId;//组织ID @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false, length = 32) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "description", unique = false, nullable = true, insertable = true, updatable = true, length = 255) public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Column(name="type") public int getType() { return type; } public void setType(int type) { this.type = type; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "node_id", unique = false, nullable = true, insertable = true, updatable = true) public String getNodeId() { return nodeId; } public void setNodeId(String nodeId) { this.nodeId = nodeId; } @Column(name = "create_id", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getCreatorId() { return creatorId; } public void setCreatorId(String creatorId) { this.creatorId = creatorId; } @Column(name = "password", unique = false, nullable = true, insertable = true, updatable = true, length = 255) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "organization_id") public SysOrganization getOrganization() { return organization; } public void setOrganization(SysOrganization organization) { this.organization = organization; } @Column(name = "phone", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name = "mobile_phone", unique = false, nullable = true, insertable = true, updatable = true, length = 20) public String getMobilePhone() { return mobilePhone; } public void setMobilePhone(String mobilePhone) { this.mobilePhone = mobilePhone; } @Column(name = "email", unique = false, nullable = true, insertable = true, updatable = true, length = 60) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name = "login_name", unique = false, nullable = true, insertable = true, updatable = true) public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @Column(name = "create_time", unique = false, nullable = true, insertable = true, updatable = true) public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Column(name = "update_time", unique = false, nullable = true, insertable = true, updatable = true) public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Column(name = "admin_type") public Integer getAdminType() { return adminType; } public void setAdminType(Integer adminType) { this.adminType = adminType; } @Transient public Set<String> getRoleIds() { return roleIds; } public void setRoleIds(Set<String> roleIds) { this.roleIds = roleIds; } @Transient public String getRoleIdS() { return roleIdS; } public void setRoleIdS(String roleIdS) { this.roleIdS = roleIdS; } @Transient public String getOrgName() { if(organization!=null){ return organization.getName(); } return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } @Transient public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } /* @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_USER_ROLE", schema = "", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }) */ @Transient public Set<SysRole> getRoles() { return roles; } public void setRoles(Set<SysRole> roles) { this.roles = roles; } @Transient public Long getOrgId() { return orgId; } public void setOrgId(Long orgId) { this.orgId = orgId; } @Override public String toString() { return "SysUser [loginName=" + loginName + ", createTime=" + createTime + "]"; } }
作者: lost blog
出处: http://www.cnblogs.com/JAYIT/
关于作者:专注服务器端开发
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题, 可邮件(sawyershaw@qq.com)咨询.