在用户角色权限设计中,我们使用复合主键来来保证唯一性
1.数据库
CREATE TABLE `sys_role_detail` ( `roleid` int(11) DEFAULT NULL COMMENT '角色id', `menuid` int(11) DEFAULT NULL COMMENT '菜单id', `orgcode` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.实体类的联合主键
联合主键满足条件:
必须实现Serializable
必须有默认的 public无参数的构造方法、必须覆盖 equals和 hashCode 方法,这些要求与使用复合主键的要求相同
将嵌入式主键类使用 @Embeddable 标注,表示这个是一个嵌入式类。
package com.datalook.model; import javax.persistence.Column; import javax.persistence.Embeddable; /** * SysRoleDetailId entity. */ @SuppressWarnings("serial") @Embeddable public class SysRoleDetailId implements java.io.Serializable { // Fields private Integer roleid; private Integer menuid; // Constructors /** default constructor */ public SysRoleDetailId() { } /** full constructor */ public SysRoleDetailId(Integer roleid, Integer menuid) { this.roleid = roleid; this.menuid = menuid; } // Property accessors @Column(name = "roleid") public Integer getRoleid() { return this.roleid; } public void setRoleid(Integer roleid) { this.roleid = roleid; } @Column(name = "menuid") public Integer getMenuid() { return this.menuid; } public void setMenuid(Integer menuid) { this.menuid = menuid; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof SysRoleDetailId)) return false; SysRoleDetailId castOther = (SysRoleDetailId) other; return ((this.getRoleid() == castOther.getRoleid()) || (this .getRoleid() != null && castOther.getRoleid() != null && this.getRoleid().equals( castOther.getRoleid()))) && ((this.getMenuid() == castOther.getMenuid()) || (this .getMenuid() != null && castOther.getMenuid() != null && this.getMenuid() .equals(castOther.getMenuid()))); } public int hashCode() { int result = 17; result = 37 * result + (getRoleid() == null ? 0 : this.getRoleid().hashCode()); result = 37 * result + (getMenuid() == null ? 0 : this.getMenuid().hashCode()); return result; } @Override public String toString() { return "SysRoleDetailId [roleid=" + roleid + ", menuid=" + menuid + "]"; } }
3.实体
用@EmbeddedId和@AttributeOverrides 属性映射
package com.datalook.model; import java.sql.Timestamp; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Transient; import com.newcapec.model.AbstractDataLookModel; /** * SysRoleDetail entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "sys_role_detail") public class SysRoleDetail extends AbstractDataLookModel { // Constructors /** default constructor */ public SysRoleDetail() { } /** constructor */ public SysRoleDetail(SysRoleDetailId sysRoleDetailId) { this.sysRoleDetailId = sysRoleDetailId; } private static final long serialVersionUID = -4244160679944459020L; private SysRoleDetailId sysRoleDetailId; private String orgcode; // Property accessors @Column(name = "orgcode", length = 32) public String getOrgcode() { return this.orgcode; } public void setOrgcode(String orgcode) { this.orgcode = orgcode; } @EmbeddedId @AttributeOverrides( { @AttributeOverride(name = "roleid", column = @Column(name = "roleid")), @AttributeOverride(name = "menuid", column = @Column(name = "menuid")) }) public SysRoleDetailId getSysRoleDetailId() { return this.sysRoleDetailId; } public void setSysRoleDetailId(SysRoleDetailId sysRoleDetailId) { this.sysRoleDetailId = sysRoleDetailId; } @Override public String toString() { return "SysRoleDetail [sysRoleDetailId=" + sysRoleDetailId + ", orgcode=" + orgcode + "]"; } }