首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频我的文字版实践笔记,本人用此来加强巩固自己开发知识,如有网友转载,请注明。谢谢。
一 之前我们写了两个功能,这两个功能之间没有什么关联关系。下面我们做的用户管理功能将和之前的两个功能有相应的关联关系,所以要先匹配之间的关联关系,然后才能够进行代码的编写,下图就是三个功能类之间的模型图,这有益于外面分析功能间的关系和编写修改功能代码:
二 将上面三个实体类全部写出来,具体如下:

1 package cn.clear.oa.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class User { 7 private Long id; 8 private String name; 9 private String loginName; 10 private String gender; 11 private String phoneNumber; 12 private String email; 13 private String password; 14 private String description; 15 private Department department; 16 private Set<Role> roles = new HashSet<Role>(); 17 public Set<Role> getRoles() { 18 return roles; 19 } 20 public void setRoles(Set<Role> roles) { 21 this.roles = roles; 22 } 23 public Long getId() { 24 return id; 25 } 26 public void setId(Long id) { 27 this.id = id; 28 } 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 public String getLoginName() { 36 return loginName; 37 } 38 public void setLoginName(String loginName) { 39 this.loginName = loginName; 40 } 41 public String getGender() { 42 return gender; 43 } 44 public void setGender(String gender) { 45 this.gender = gender; 46 } 47 public String getPhoneNumber() { 48 return phoneNumber; 49 } 50 public void setPhoneNumber(String phoneNumber) { 51 this.phoneNumber = phoneNumber; 52 } 53 public String getEmail() { 54 return email; 55 } 56 public void setEmail(String email) { 57 this.email = email; 58 } 59 public String getPassword() { 60 return password; 61 } 62 public void setPassword(String password) { 63 this.password = password; 64 } 65 public String getDescription() { 66 return description; 67 } 68 public void setDescription(String description) { 69 this.description = description; 70 } 71 public Department getDepartment() { 72 return department; 73 } 74 public void setDepartment(Department department) { 75 this.department = department; 76 } 77 78 79 }

1 package cn.clear.oa.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Role { 7 8 private Long id; 9 private String name; 10 private String description; 11 private Set<User> users = new HashSet<User>(); 12 public Long getId() { 13 return id; 14 } 15 public void setId(Long id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public String getDescription() { 25 return description; 26 } 27 public void setDescription(String description) { 28 this.description = description; 29 } 30 public Set<User> getUsers() { 31 return users; 32 } 33 public void setUsers(Set<User> users) { 34 this.users = users; 35 } 36 37 38 }

1 package cn.clear.oa.domain; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Department { 7 8 private Long id; 9 private String name; 10 private String description; 11 private Department parent; 12 private Set<User> users = new HashSet<User>(); 13 private Set<Department> children = new HashSet<Department>(); 14 15 public Department getParent() { 16 return parent; 17 } 18 public void setParent(Department parent) { 19 this.parent = parent; 20 } 21 public Set<User> getUsers() { 22 return users; 23 } 24 public void setUsers(Set<User> users) { 25 this.users = users; 26 } 27 public Set<Department> getChildren() { 28 return children; 29 } 30 public void setChildren(Set<Department> children) { 31 this.children = children; 32 } 33 public Long getId() { 34 return id; 35 } 36 public void setId(Long id) { 37 this.id = id; 38 } 39 public String getName() { 40 return name; 41 } 42 public void setName(String name) { 43 this.name = name; 44 } 45 public String getDescription() { 46 return description; 47 } 48 public void setDescription(String description) { 49 this.description = description; 50 } 51 52 53 }
三 分析三个类的关联关系,在映射文件中配置相应的属性,我将映射文件内容表于下面:

1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.clear.oa.domain"> 7 <class name="User" table="oa_user"> 8 <id name="id"><generator class="native"/></id> 9 <property name="name"/> 10 <property name="loginName"/> 11 <property name="gender"/> 12 <property name="phoneNumber"/> 13 <property name="email"/> 14 <property name="password"/> 15 <property name="description"/> 16 <!-- department属性,本类与Department的多对1关系 --> 17 <many-to-one name="department" class="Department" column="departmentId"></many-to-one> 18 <!-- roles属性,本类与Role的多对多关系 --> 19 <set name="roles" table="oa_user_role"> 20 <key column="userId"></key> 21 <many-to-many class="Role" column="roleId"></many-to-many> 22 </set> 23 </class> 24 </hibernate-mapping>

1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.clear.oa.domain"> 7 <class name="Role" table="oa_role"> 8 <id name="id"> 9 <generator class="native" /> 10 </id> 11 <property name="name" /> 12 <property name="description" /> 13 <!-- users属性,本类与User的多对多 --> 14 <set name="users" table="oa_user_role"> 15 <key column="roleId"></key> 16 <many-to-many class="User" column="userId"></many-to-many> 17 </set> 18 </class> 19 </hibernate-mapping>

1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.clear.oa.domain"> 7 <class name="Department" table="oa_department"> 8 <id name="id"><generator class="native"/></id> 9 <property name="name"/> 10 <property name="description"/> 11 <!-- users属性,本类与User的1对多 --> 12 <set name="users"> 13 <key column="departmentId"></key> 14 <one-to-many class="User"/> 15 </set> 16 <!-- parent属性,本类与(上级)Department的多对1 --> 17 <many-to-one name="parent" class="Department" column="parentId"></many-to-one> 18 <!-- children属性,本类与(下级)Department的1对多 --> 19 <set name="children"> 20 <key column="parentId"></key> 21 <one-to-many class="Department"/> 22 </set> 23 </class> 24 </hibernate-mapping>
总结出映射文件的写法,下面的内容就来概括:
例:<!-- users属性,本类与User类的一对多 -->
格式:a属性,本类与b的c。
解释:a为属性名,b为关联的对象名,c为对应的关系。
模板(以后在填写多表映射时可以以以此为模板):
1)多对一:
<many-to-one name="a" class="b" column="a+Id"></many-to-one>
2)一对多:
<set name="a">
<key column="对应多对一中的column"></key>
<one-to-many class="b"/>
</set>
3)多对多:
<set name="a" table="oa_本类类名(小写)_b类名(小写)">
<key column="本类类名小写+Id"></key>
<many-to-many class="b" column="b类名+Id"></many-to-many>
</set>
四 以上做完后启动之前的测试类TestSpring.java中的测试SessionFactory的方法进行自动建表,如不报错,去数据库看看是否正确。