1. 程式人生 > >表與表之間關係 --------實體與實體之間關係

表與表之間關係 --------實體與實體之間關係

                                                                           表與表之間關係

                                                                                                       --------實體與實體之間關係

         我們知道的表與表之間有三種關係1.一對一  2.多對一  3.多對多

         首先我們來講一下第二種多對一的關係:其中有多對一,和一對多。

我們用一個例項來說明:

         Department和Employee  這個我們知道是一個一對多的關係:

         我們說一個簡單點的  在department中有兩個屬性  id 和name

                                                        在employee中有三個欄位   id、name和Deployment 的depart

我們首先說一下Emplyee物件中的第三個欄位。我們為什麼要這樣寫呢?為什麼外間要這樣寫呢?我們知道如果定義成一個int型別的也能用!但是呢?我們只能得到int這一個欄位的型別,但是如果我們要想得到其他的如:emplyee中的name呢?不是得不到而是很麻煩?

         所以我們用一個類定義一個屬性,這樣我們得到裡面的內容我們一起直接用get方法得到!原因只在於此

 這是多對一

         我們還是看看Department這個類是怎麼寫的吧!

public classDepartment {

   private int id;

   private String name;

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

}

其實就是get和set方法。

我們再來看一下這個的xml檔案

<hibernate-mapping package="domain"><!-- 包明子就是實體類在那個包裡面 -->

   <class name="Department" table="department">     

      <id name="id"column="id">                       

         <generator class="native"/>

      </id>

      <property name="name" not-null="true"/>    

   </class>

</hibernate-mapping>

因為這個類中沒有所謂的外來鍵,所以呢?我們沒有特殊的地方

我們再來看看這個employee這個類是怎麼寫的把!

public classEmployee {

   private int id;

   private String name;

   private Department depart;

   public Department getDepart(){

      return depart;

   }

   public void setDepart(Departmentdepart) {

      this.depart = depart;

   }

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

}

其實我們看清楚點,也就是get和set方法!但是裡面多了一個Deployment型別的物件。

<hibernate-mapping package="domain">

   <class name="Employee" table="employee">     

      <id name="id"column="id">                       

         <generator class="native"/>

      </id>

      <property name="name" not-null="true"/>

      <many-to-one name="depart" column="depart_id"/>

   </class>

</hibernate-mapping>

看看這個類,我們清楚了我們在其中看到了一個特別的地方也就是那個<many-to-one>這個標籤

我們來解釋一下!其中的name就是我們在Employee中定義的一個型別是Deployment我們後面的column就是對應的表中的外間的欄位名字。

我們在我們的射影檔案裡面配置一下ok了。

我們在設計一個測試的類:

static Department add(){

      Session session=null;

      Transaction tx=null;

      try{

         session=hibernateUtil.getSession();

         tx=session.beginTransaction();

         Department department=newDepartment();

         department.setName("depart name");

         Employee employee=newEmployee();

         employee.setName("emp name");

         employee.setDepart(department);//建立物件模型,讓兩個物件關聯

在這裡我們順序最好別別變,因為如果變了位置不是不能用。而是會多了一個update語句,影響了速度。

 
        

         session.save(department);

         session.save(employee);

         tx.commit();

         return department;

      }finally{

         if (session!=null) {

            session.close();

         }

      }

   }

其中的方法是add();我們很清楚了基本的操作。我們從構造department物件開始吧!首先,我們構建一個department的物件然後設定他的名字。沒有什麼不同。我們在往下看我們是構造了一個Employee這個物件。然後設定姓名,------------------這是重點了------------我們有設定了Depart的屬性----   我們首先必須存入這個department物件因為這個是一的哪一方。然後我們在設定多的哪一方。這樣才能成功匯入、這就是插入了記錄。並且兩個記錄是關聯的。

         我們再來看一下個的查詢吧!

      static Employee query(int empId){

      Sessionsession=null;

      Transactiontx=null;

      try{

         session=hibernateUtil.getSession();

         tx=session.beginTransaction();

         Employee  employee=(Employee) session.get(Employee.class, empId);

         System.out.println(employee.getDepart().getName());

         tx.commit();

         return employee;

      }finally{

         if (session!=null) {

            session.close();

         }

      }

   }

其他的都一樣只是在加入一個函式即可就是我們的查詢函式,這個函式呢?就是用員工查員工所在的部門!這樣我們就能找到那個部門,如果要是差自己的編號呢?就是單表了,我們在這裡用的是多表查詢

這就是多對一的例項!

下面我們再來看一下一對多的實力吧!

我們還是用員工和部門這個實力吧!

是這樣的我們這次用set集合為什麼要用set呢?

   答:在別的地方沒有看到。我想應該是主鍵吧不能相同,而set集合正是不

能相同的元素在一起的,所以我們還是用這個set

首先我們還是來寫一下department這個類吧!

package domain;

import java.util.Set;

public classDepartment {

   private int id;

   private String name;

   private Set<Employee> emps;

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

   public Set<Employee>getEmps() {

      return emps;

   }

   public voidsetEmps(Set<Employee> emps) {

      this.emps = emps;

   }

}

在我們這裡看來沒有什麼變化!只是多了一個set的集合,然後呢set集合必須指定實體就是那個Employee

我們看一下這個類所對應的xml檔案吧

<hibernate-mapping package="domain">

   <class name="Department">     

      <id name="id"column="id">                       

         <generator class="native"/>

      </id>

      <property name="name" not-null="true"/>

      <set name="emps">

         <key column="depart_id"/>

         <one-to-many class="Employee"/>

      </set>

   </class>

</hibernate-mapping>

分析一下其實就是多了一個set的標籤吧!我們看一下內容name就是那個set的名字,裡面的key標籤就是那個要建立的外來鍵?在哪裡建立呢?  看下面在多的哪一方也就是Employee所對應的那個表

我們再來看一下Employee這個類

package domain;

public classEmployee {

   private int id;

   private String name;

   private Department depart;

   public Department getDepart(){

      return depart;

   }

   public void setDepart(Departmentdepart) {

      this.depart = depart;

   }

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

}

看見了吧!其實是一點都沒有變

在看一下那個xml檔案

<hibernate-mapping package="domain"><!-- 包明子就是實體類在那個包裡面 -->

   <class name="Employee">     

      <id name="id"column="id">                       

         <generator class="native"/>

      </id>

      <property name="name" not-null="true"/>

   <!-- <many-to-onename="depart" column="depart_id"/> -->

   </class>

</hibernate-mapping>

相信大家都看見了,原來是吧那個多對一的那個標籤移除了

我們再來看兩個方法 還是插入和查詢的

   static Department add(){

      Session session=null;

      Transactiontx=null;

      try{

         session=hibernateUtil.getSession();

         tx=session.beginTransaction();

         Departmentdepartment=newDepartment();

         department.setName("depart name");

         Employeeemployee1=newEmployee();

         employee1.setName("emp name1");

         employee1.setDepart(department);//建立物件模型,讓兩個物件關聯

         Employeeemployee2=newEmployee();

         employee2.setName("emp name2");

         employee2.setDepart(department);//建立物件模型,讓兩個物件關聯

         Set<Employee>se=newHashSet<Employee>();

         se.add(employee1);

         se.add(employee2);

         department.setEmps(se);

         session.save(employee1);

         session.save(employee2);

         session.save(department);

         System.out.println("===========");

         tx.commit();

         return department;

      }finally{

         if (session!=null) {

            session.close();

         }

      }

   }

   static DepartmentqueryDepartment(intdepartId){

      Sessionsession=null;

      Transactiontx=null;

      try{

         session=hibernateUtil.getSession();

         tx=session.beginTransaction();

         Departmentdepart=(Department) session.get(Department.class, departId);

         System.out.println(depart.getEmps().size());

         tx.commit();

         return depart;

      }finally{

         if (session!=null) {

            session.close();

         }

      }

   }

哎!這裡我們大家都看清楚了都一樣嗎!

但是大家在這裡都要想清楚啊!

我們在多對一中查詢的時候是用多的哪一方查詢的少的

   在一對多中查詢的是後是衝少的哪一方查詢多的哪一方啊!

在今天的最後我們來講一下一對一: