Hibernate一对1、一对多、多对多表间关系

1. 为何要学习关系关系?
数据库表间存在若干关系
 万事万物也存在若干关系
2. 对象间有几种关联关系
单向 :你中有我,我中没你
双向:你中有我,我中有您。
3. 关系型数据与对象型数据
数据库:存放关系模型的数据
java:存放对象模型的数据
4. 单向一对多
 部门与员工

 

 Emp.javahtml

public  class  Emp{
     private int id;
     private String name;
}
 Dept.java
public  class  Dept{
     private int id;
     private String name;
     Set<Emp>  emps  = new HashSet<Emp>();
}
 Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
 </class>
  Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
        <set name="emps" cascade="all" inverse="true">
        	<key column="dept_id"></key>
        	<one-to-many class="com.gec.cn.dao.domain.Emp" />
        </set>
 </class>
5. 单向多对一

  Emp.java
public  class  Emp{
     private int id;
     private String name;
     private Dept dept;
}
 Dept.java
public  class  Dept{
     private int id;
     private String name;
}
  Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
<many-to-one name="dept" column="dept_id" class="com.gec.cn.dao.domain.Dept"></many-to-one>
 </class>
 Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
 </class>
6  双向多对一(一对多)

 Dept.java
public  class  Dept{
     private int id;
     private String name;
     Set<Emp>  emps  = new HashSet<Emp>();
}
  Dept.java
public  class  Emp{
     private int id;
     private String name;
     private Dept dept;
}
 Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
<many-to-one name="dept" column="dept_id" class="com.gec.cn.dao.domain.Dept"></many-to-one>
 </class>
  Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
		 <set name="emps" cascade="all" inverse="true">
        	<key column="dept_id"></key>
        	<one-to-many class="com.gec.cn.dao.domain.Emp" />
        </set>
 </class>
7. 多对多
老师与学生
 
 Teacher.java
public  class  Teacher{
     private int id;
     private String name;
     Set<Student>  stus  = new HashSet<Student>();
}
 Student.java
public  class  Student{
     private int id;
     private String name;
     Set<Teacher>   teas = new HashSet<Teacher>();
}
  Student.hbm.xml
<class name="com.gec.cn.dao.domain.Student" table="student">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
        <property name="sex" column="sex" type="string"/>
        <property name="birthday" column="birthday" type="date"/>
        
        <set name="teachers" table="tea_stu">
        <key column="stu_id"></key>
          <many-to-many class="com.gec.cn.dao.domain.Teacher" column="tea_id"></many-to-many>
        </set>
</class>
  Teacher.hbm.xml
<class name="com.gec.cn.dao.domain.Teacher" table="teacher">
        <id name="id" column="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="string"/>
        <property name="sex" column="sex" type="string"/>
        <property name="address" column="address" type="string"/>
        
        <set name="students" table="tea_stu">
        <key column="tea_id"></key>
          <many-to-many class="com.gec.cn.dao.domain.Student" column="stu_id"></many-to-many>
        </set> 
</class>
8. 一对一
     人与身份证
(1)按外键映射
 
 Person.java
public  class  Pserson{
     private int id;
     private String name;
     private IdCard idcard;
}
 IdCard.java
public  class  IdCard{
     private int id;
     private String name;
     private Person person;
}
  Person.hbm.xml
<class name="com.gec.cn.dao.daomain.Person" table="person">
        <id name="id" column="id" type="long">
            <generator class="increment" />
        </id> 
        <many-to-one name="idcard" 
        class="com.gec.cn.dao.daomain.IDCard"
        column="card_id"
        cascade="all" unique="true"/>
 </class>
 IdCard.hbm.xml
<class name="com.gec.cn.dao.daomain.IDCard" table="idcard">
        <id name="id" column="id" type="long">
            <generator class="increment" />
        </id>
        <property name="name" column="name" type="string"/>
        <property name="num" column="num" type="string"/>
        <property name="address" column="address" type="string"/> 
        <one-to-one name="person" 
        class="com.gec.cn.dao.daomain.Person"
        cascade="all"/>
   </class>
(2)按主键映射
 
 Person.java
public  class  Pserson{
     private int id;
     private String name;
     private IdCard idcard;
}
 IdCard.java
public  class  IdCard{
     private int id;
     private String name;
     private Person person;
}
 Person.hbm.xml
<class name="com.gec.cn.dao.daomain.Person" table="tb_person">
        <id name="id" column="id" type="long">
            <generator class="foreign">
            <param name="property">card</param>
            </generator>
        </id>
<property name="name" column="name" type="string"></property>
<one-to-one name="card" class="com.gec.cn.dao.daomain.Card" constrained="true"></one-to-one>
</class>
  IdCard.hbm.xml
<class name="com.gec.cn.dao.daomain.Card" table="tb_card">
        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>
        <property name="num" column="num" type="string"/> 
        <one-to-one name="person" 
        class="com.gec.cn.dao.daomain.Person"
        cascade="all"/>
 </class>