hiberate 映射关系 详解

 

  在我们平时所学的关系型数据库中,我们会大量处理表与表之间的关系,如果表比较多的话处理起来就比较繁琐了,但是hibernate给我们提供了很大的便利,这些便利让我们处理起来方便。我们所讲的源码地址:http://download.csdn.net/detail/yfyzy/8953565

  如果我们把表的每条记录当成一个对象的话,那我们我们的映射关系可以分为四种

  1)一对一

  2)一对多

  3)多对一

  4)多对多

准备工作

现在我们假设有四个表,员工表,部门表,办工作表,职位表,有如下映射关系

  1)多个员工对应一个部门

  2)一个部门对应多个员工

  3)一个员工对应多个职位(这里我们假设一个人有多大能力,就要承担多大责任,所有就先多对多啦~O(∩_∩)O~)。

  4)一个职位对应多个员工

  5)一个员工对应一张办公桌,一张办公桌对应一个员工。

1.多对一 与 一对多

  通过上面的描述我们知道员工多个员工对应着一个部门

员工实体(多方)

 1 package entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Emp {
 7  
 8     private int id ;        //员工id
 9     private String name ;   //员工姓名
10     private int age ;    //员工年龄
11     private Dept dept;   //员工部门 ,注意这里是Dept对象哦,不是id哦
12 
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public int getAge() {
26         return age;
27     }
28     public void setAge(int age) {
29         this.age = age;
30     }
31     public Dept getDept() {
32         return dept;
33     }
34     public void setDept(Dept dept) {
35         this.dept = dept;
36     }
37     public Set<Position> getPositions() {
38         return positions;
39     }
40     public void setPositions(Set<Position> positions) {
41         this.positions = positions;
42     }
43     
44     }
45     
46 }

员工实例与数据库表映射关系配置(多方)

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4     
 5  <!-- 员工实体类Emp与员工表t_emp的映射  -->
 6 <hibernate-mapping >
 7     <class name="entity.Emp" table="t_emp">
 8     
 9        <!-- 主键id配置 -->
10         <id name="id" column="id">
11         <!-- 
12             数据库的主键生成策略  native配置为使用数据库自带的生成策略  关于参数详解参考:http://www.cnblogs.com/yfyzy/p/4675041.html
13         -->
14             <generator class="native"></generator>
15         </id>
16         
17         <!-- 属性设置 -->
18         <property name="name" column="name" >
19         </property>
20         <property name="age" column="age">
21         </property>
22         
23         <!-- 多对一配置,多个员工对应一个部门 -->
24         <many-to-one  name = "dept" class="entity.Dept" 
25         column = "dept_id" cascade="save-update">
26         </many-to-one>
27           
28     </class>
29 </hibernate-mapping>

部门实体(一方

 1 package entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Dept {
 7     private int id; // 部门id
 8     private String dname; // 部门名称
 9     private Set<Emp> emps = new HashSet<Emp>();  //该部门的员工,是一个集合。
10 
11     public int getId() {
12         return id;
13     }
14 
15     public void setId(int id) {
16         this.id = id;
17     }
18 
19     public String getDname() {
20         return dname;
21     }
22 
23     public void setDname(String dname) {
24         this.dname = dname;
25     }
26     
27     public Set<Emp> getEmps() {
28         return emps;
29     }
30 
31     public void setEmps(Set<Emp> emps) {
32         this.emps = emps;
33     }
34 
35 }

部门实体与数据表的映射(一方)

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4    
 5  <!-- 部门实体类Dept与员工表t_dept的映射  -->
 6   <hibernate-mapping>
 7       <class name = "entity.Dept" table = "t_dept">
 8       
 9             <!-- 主键id配置 -->
10            <id name="id" column="id">
11         <!-- 
12             数据库的主键生成策略  native配置为使用数据库自带的生成策略  关于参数详解参考:http://www.cnblogs.com/yfyzy/p/4675041.html
13         -->
14             <generator class="native"></generator>
15         </id>
16         
17          <!-- 属性设置 -->
18         <property name="dname" column="dname" >
19         </property>
20         
21         <!-- 一对多配置,一个部门有多个员工,所以是一个集合 -->
22         <set name = "emps" cascade = "all" inverse="true">
23                <key column = "dept_id"></key>
24                <one-to-many class="entity.Emp"></one-to-many>
25         </set>
26         
27       </class>
28   </hibernate-mapping>

 2.多对多

  这里我们假设的是,一个员工对应多个职位,一个职位有多个员工。

员工实体类

 1 package entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Emp {
 7  
 8     private int id ;        //员工id
 9     private String name ;   //员工姓名
10     private int age ;    //员工年龄
11     private Dept dept;   //员工部门 ,注意这里是Dept对象哦,不是id哦
12     private Set<Position> positions = new HashSet<Position>();  //注意这里是一个Position职位集合,我们这里假设一个员工可以对应多个职位
13     
14     public int getId() {
15         return id;
16     }
17     public void setId(int id) {
18         this.id = id;
19     }
20     public String getName() {
21         return name;
22     }
23     public void setName(String name) {
24         this.name = name;
25     }
26     public int getAge() {
27         return age;
28     }
29     public void setAge(int age) {
30         this.age = age;
31     }
32     public Dept getDept() {
33         return dept;
34     }
35     public void setDept(Dept dept) {
36         this.dept = dept;
37     }
38     public Set<Position> getPositions() {
39         return positions;
40     }
41     public void setPositions(Set<Position> positions) {
42         this.positions = positions;
43     }
44 }

员工实体与数据库映射关系

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4     
 5  <!-- 员工实体类Emp与员工表t_emp的映射  -->
 6 <hibernate-mapping >
 7     <class name="entity.Emp" table="t_emp">
 8     
 9        <!-- 主键id配置 -->
10         <id name="id" column="id">
11         <!-- 
12             数据库的主键生成策略  native配置为使用数据库自带的生成策略  关于参数详解参考:http://www.cnblogs.com/yfyzy/p/4675041.html
13         -->
14             <generator class="native"></generator>
15         </id>
16         
17         <!-- 属性设置 -->
18         <property name="name" column="name" >
19         </property>
20         <property name="age" column="age">
21         </property>
22         
23         <!-- 多对一配置,多个员工对应一个部门 -->
24         <many-to-one  name = "dept" class="entity.Dept" 
25         column = "dept_id" cascade="save-update">
26         </many-to-one>
27         
28         <!-- 多对多配置,一个员工可以对应多个职位 -->
29         <set name="positions" inverse="false" cascade="save-update" table="t_emp_position">
30             <key column="emp_id"></key>
31             <many-to-many class = "entity.Position" column = "position_id">
32             </many-to-many>
33         </set>
34         
35     </class>
36 </hibernate-mapping>

职位实体

 1 package entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Position {
 7     private int id ;   //职位id
 8     private String positionName ;  //职位名称
 9     private Set<Emp> emps = new HashSet<Emp>();     //存放这种职位的员工
10     
11     public Set<Emp> getEmps() {
12         return emps;
13     }
14     public void setEmps(Set<Emp> emps) {
15         this.emps = emps;
16     }
17     public int getId() {
18         return id;
19     }
20     public void setId(int id) {
21         this.id = id;
22     }
23     public String getPositionName() {
24         return positionName;
25     }
26     public void setPositionName(String positionName) {
27         this.positionName = positionName;
28     }  
30     
31 }

实体与数据库表的映射关系

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4     
 5 <!-- 职位实体类Emp与员工表t_position的映射  -->
 6 <hibernate-mapping>
 7     <class name="entity.Position" table="t_position">
 8     
 9          <!-- 主键id配置 -->
10         <id name="id" column = "id">
11             <generator class="native"></generator>
12         </id>    
13         
14          <!-- 属性设置 -->
15         <property name="positionName" column="position_name"/>
16         
17           <!-- 多对多配置,一个职位可以对应多个员工 -->
18         <set name="emps" inverse="true" cascade="save-update" table="t_emp_position">
19             <key column="position_id"></key>
20             <many-to-many class = "entity.Emp" column = "emp_id">
21             </many-to-many>
22         </set>
23                  
24     </class>
25     
26 </hibernate-mapping>

3.一对一

   一个员工对应一个办公桌,一张办公桌也只对应一个员工

一对关联的方式有两种

  1)外键关联

  2)主键关联

3.1外键关联

  本质上是一对多的蜕化形式。在<many-to-one>元素中设置属性unique=”true”就变成了一对一。

员工实体

 1 package entity;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Emp {
 7  
 8     private int id ;        //员工id
 9     private String name ;   //员工姓名
10     private int age ;    //员工年龄
11     private Dept dept;   //员工部门 ,注意这里是Dept对象哦,不是id哦
12     private Set<Position> positions = new HashSet<Position>();  //注意这里是一个Position职位集合,我们这里假设一个员工可以对应多个职位。
13     private Desk desk;  //办工桌对象
14     
15     public int getId() {
16         return id;
17     }
18     public void setId(int id) {
19         this.id = id;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public int getAge() {
28         return age;
29     }
30     public void setAge(int age) {
31         this.age = age;
32     }
33     public Dept getDept() {
34         return dept;
35     }
36     public void setDept(Dept dept) {
37         this.dept = dept;
38     }
39     public Set<Position> getPositions() {
40         return positions;
41     }
42     public void setPositions(Set<Position> positions) {
43         this.positions = positions;
44     }
45     public Desk getDesk() {
46         return desk;
47     }
48     public void setDesk(Desk desk) {
49         this.desk = desk;
50     }
51     
52 }

员工实体类Emp与数据库表t_emp的映射关系

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4     
 5  <!-- 员工实体类Emp与员工表t_emp的映射  -->
 6 <hibernate-mapping >
 7     <class name="entity.Emp" table="t_emp">
 8     
 9        <!-- 主键id配置 -->
10         <id name="id" column="id">
11         <!-- 
12             数据库的主键生成策略  native配置为使用数据库自带的生成策略  关于参数详解参考:http://www.cnblogs.com/yfyzy/p/4675041.html
13         -->
14             <generator class="native"></generator>
15         </id>
16         
17         <!-- 属性设置 -->
18         <property name="name" column="name" >
19         </property>
20         <property name="age" column="age">
21         </property>
22         
23         <!-- 多对一配置,多个员工对应一个部门 -->
24         <many-to-one  name = "dept" class="entity.Dept" 
25         column = "dept_id" cascade="save-update">
26         </many-to-one>
27         
28         <!-- 多对多配置,一个员工可以对应多个职位 -->
29         <set name="positions" inverse="false" cascade="save-update" table="t_emp_position">
30             <key column="emp_id"></key>
31             <many-to-many class = "entity.Position" column = "position_id">
32             </many-to-many>
33         </set>
34         
35         <!-- 一对一配置,设置属性unique=”true”就变成了一对一  -->
36         <many-to-one name="desk" class="entity.Desk" column="desk_id" unique="true"></many-to-one>
37         
38     </class>
39 </hibernate-mapping>

办工桌实体类

 1 package entity;
 2 
 3 public class Desk {
 4 
 5     private int id ; // 办公桌id
 6     private String deskName; //办工桌名称
 7     private Emp emp ;  //员工,这张桌子的拥有者
 8     
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getDeskName() {
16         return deskName;
17     }
18     public void setDeskName(String deskName) {
19         this.deskName = deskName;
20     }
21     public Emp getEmp() {
22         return emp;
23     }
24     public void setEmp(Emp emp) {
25         this.emp = emp;
26     }
27 }

办工桌实体类Desk与数据库表t_desk的映射

 1 <!DOCTYPE hibernate-mapping PUBLIC 
 2     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4    
 5  <!-- 部门实体类Dept与员工表t_dept的映射  -->
 6   <hibernate-mapping>
 7       <class name = "entity.Desk" table = "t_desk">
 8       
 9             <!-- 主键id配置 -->
10            <id name="id" column="id">
11         <!-- 
12             数据库的主键生成策略  native配置为使用数据库自带的生成策略  关于参数详解参考:http://www.cnblogs.com/yfyzy/p/4675041.html
13         -->
14             <generator class="native"></generator>
15         </id>
16         
17          <!-- 属性设置 -->
18         <property name="deskName" column="desk_name" >
19         </property>
20         
21         <!-- 一对一配置,一个员工对应一张桌子   -->
22         <one-to-one name="emp" class="entity.Emp" cascade="all" >
23         </one-to-one>
24         
25       </class>
26   </hibernate-mapping>

3.2主键关联

   主键关联即其中一个表的主键参照另外一张表的主键而建立起一对一关联关系

员工实体类Emp与数据库表t_emp的映射关系修改如下

1       <!-- 一对一主键关联配置,一个员工对应一张桌子   -->
2      <one-to-one name="desk" class="entity.Desk"></one-to-one>        

办工桌实体类Desk与数据库表t_desk修改如下

1   <!-- 一对一配置,一个员工对应一张桌子   -->
2 <one-to-one name="emp" class="entity.Emp" cascade="all" >
3         </one-to-one>
原文地址:https://www.cnblogs.com/yfyzy/p/4690559.html