配置用户、角色、部门之间的关系
1.一个用户有一个部门,一个部门有多个用户,也就是说用户和部门是多对一的关系。
2.一个用户有多种角色,一个角色有多个用户,即用户和角色是多对多的关系。
3.一个部门的上级部门只有一个,下级部门可以有多个。
User.hbm.xml的配置文件
<class name="cn.itcast.oa.domain.User" table="itcast_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" length="32"></property>
<property name="description"></property>
<property name="loginName"></property>
<property name="phone"></property>
<property name="email"></property>
<property name="password"></property>
<property name="gender"></property>
<!--配置用户和岗位的多对多关系
name:引用属性名
table:中间表名
key.column:外键,别人引用我的外键列名
class:关联类的类名
column:外键,我引用别人的关系的外键列名
-->
<set name="roles" table="itcast_user_role">
<key column="userId"></key>
<many-to-many class="cn.itcast.oa.domain.Role"
column="roleId"></many-to-many>
</set>
<!--配置用户和部门的多对一关系
name:引用属性名
class:与我关联的类名
column:外键列名
-->
<many-to-one name="department"
class="cn.itcast.oa.domain.Department" column="departmentId"></many-to-one>
</class>
Role.hbm.xml配置文件
<class name="cn.itcast.oa.domain.Role" table="itcast_role">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" length="32"></property>
<property name="description"></property>
<!--配置角色和用户的多对多关系
users:引用属性名
table:中间表表名
column:key.column:外键,别人引用我的外键列名
class:对应类的完整类名
column:外键,我引用别人的关系的外键列名
-->
<set name="users" table="itcast_user_role">
<key column="roleId"></key>
<many-to-many class="cn.itcast.oa.domain.User" column="userId"></many-to-many>
</set>
</class>
Department.hbm.xml的配置文件
<class name="cn.itcast.oa.domain.Department" table="itcast_department">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" length="32"></property>
<property name="description"></property>
<!-- 配置用户和部门的一对多关系 -->
<set name="users">
<key column="departmentId"></key>
<one-to-many class="cn.itcast.oa.domain.User"/>
</set>
<!-- 子部门和上级部门的多对一关系
自关联:生成一个外键自关联自己的主键
id name parentId
1 开发部 null
2 开发一部 1
3 开发二部 1
-->
<many-to-one name="parent" class="cn.itcast.oa.domain.Department" column="parentId"></many-to-one>
<!-- 上级部门和子部门之间的一对多关系 -->
<set name="children">
<key column="parentId"></key>
<one-to-many class="cn.itcast.oa.domain.Department"/>
</set>
</class>
***Department实体***
public class Department {
private Long id;
private String name;
private String description;
private Department parent;
private Set<Department> children = new HashSet<Department>();
private Set<User> users = new HashSet<User>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
***Role实体***
public class Role {
private Long id;
private String name;
private String description;
private Set<User> users = new HashSet<User>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
***User实体***
public class User {
private Long id;
private String loginName;
private String name;
private int gender;
private String phone;
private String email;
private String description;
private String password;
private Department department;
private Set<Role> roles = new HashSet<Role>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}