在java后台权限设置时需要使用联合主键来唯一标识权限
public class SystemPrivilegePK implements Serializable{
private static final long serialVersionUID = 1L;
private SystemPrivilegePK id;
private String model;
private String privilege;
public SystemPrivilegePK(){}
public SystemPrivilegePK(String module, String privilege2) {
this.model = module;
this.privilege = privilege2;
}
get和set方法省略
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result + ((privilege == null) ? 0 : privilege.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SystemPrivilegePK other = (SystemPrivilegePK) obj;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (privilege == null) {
if (other.privilege != null)
return false;
} else if (!privilege.equals(other.privilege))
return false;
return true;
}
@Override
public String toString() {
return "SystemPrivilegePK [model=" + model + ", privilege=" + privilege + "]";
}
}
xml配置为:
<class name="com.robin.main.privilege.systemprivilege.SystemPrivilegePK" table="SYSTEMPRIVILEGEPK">
<composite-id name="id" class="com.robin.main.privilege.systemprivilege.SystemPrivilegePK">
<key-property name="model" column="model" type="string"></key-property>
<key-property name="privilege" column="privilege" type="string"></key-property>
</composite-id>
</class>
public class SystemPrivilege {
private SystemPrivilegePK id;
private String name;
private Set<PrivilegeGroup> pgs = new HashSet<>();
public SystemPrivilege(){}
public SystemPrivilege(String module, String privilege, String name) {
this.id = new SystemPrivilegePK(module, privilege);
this.name = name;
}
public SystemPrivilege(SystemPrivilegePK id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SystemPrivilege other = (SystemPrivilege) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "SystemPrivilege [id=" + id + ", name=" + name + "]";
}
}
其中关于外键id的XML配置:
<composite-id name="id" class="com.robin.main.privilege.systemprivilege.SystemPrivilegePK">
<key-property name="model" column="model" type="string"></key-property>
<key-property name="privilege" column="privilege" type="string"></key-property>
</composite-id>
<set name="pgs" table="t_sp_pg">
<key>
<column name="model"></column>
<column name="privilege"></column>
</key>
<many-to-many class="com.robin.main.privilege.systemprivilege.PrivilegeGroup" column="pgs_id"></many-to-many>
</set>
public class PrivilegeGroup {
private Integer groupid;
private String name;
private Set<SystemPrivilege> sps = new HashSet<>();
private Set<Employee> employees = new HashSet<>();
public PrivilegeGroup(){}
public PrivilegeGroup(Integer groupid) {
this.groupid = groupid;
}
public void addSystemPrivilege(SystemPrivilege privilege){
this.sps.add(privilege);
}
}
xml配置为:<id name="groupid" type="java.lang.Integer">
<column name="GROUPID" />
<generator class="native" />
</id><set name="sps" table="t_sp_pg">
<key column="pgs_id"></key>
<many-to-many class="com.robin.main.privilege.systemprivilege.SystemPrivilege">
<column name="model"></column>
<column name="privilege"></column>
</many-to-many>
</set>SystemPrivilege由SystemPrivilegePK作为它的主键,SystemPrivilegePK需要实现序列化接口,并且需要实现相应的equals和hashcode方法来比较对象是否相等
863

被折叠的 条评论
为什么被折叠?



