1. 程式人生 > >hibernate的多對多

hibernate的多對多

  1. 資料庫的多對多
    1.1 資料庫中不能直接對映多對多
    處理:建立一個橋接表(中間表),將一個多對多關係轉換成兩個一對多

    //多對多關係圖 在這裡插入圖片描述

    注1:資料庫多表聯接查詢
    永遠就是二個表的聯接查詢

        A   B   C  D
           t1   C
                t2 D
                   t3
    

    注2:交叉連線
    注3:外連線:left(左)/right(右)/full(左右)
    主從表:連線條件不成立時,主表記錄永遠保留,與null匹配

        A	B	AB
        select * from A,B,AB WHERE A.aID=AB.aID and b.bid = AB.bid
        where 
        在hibernate中,你只管查詢當前表物件即可,
        hibernate會自動關聯橋表以及關聯表查詢出關聯物件
        
        Book	Category Book_category
        select * from Book b,Book_category bc,category where b.bid = bc.bid and bc.cid = c.cid
        and bid = 2
    
  2. hibernate的多對多
    2.1 hibernate可以直接對映多對多關聯關係(看作兩個一對多)

//Book實體類
public class Book {

private Integer bookId;
private String bookName;
private Float price;
//書本對應多個類別
private Set<Category> category=new HashSet<>();


public Set<Category> getCategory() {
	return category;
}
public void setCategory(Set<Category> category) {
	this.category = category;
}
public Integer getBookId() {
	return bookId;
}
public void setBookId(Integer bookId) {
	this.bookId = bookId;
}
public String getBookName() {
	return bookName;
}
public void setBookName(String bookName) {
	this.bookName = bookName;
}
public Float getPrice() {
	return price;
}
public void setPrice(Float price) {
	this.price = price;
}

}

//Category 書本類別類
 package com.zking.five.entity;
import java.util.HashSet;
import java.util.Set;

public class Category {

	private Integer categoryId;
	private String  categoryName;
	private Set<Book> book=new HashSet<>();
	
	
	public Set<Book> getBook() {
		return book;
	}
	public void setBook(Set<Book> book) {
		this.book = book;
	}
	public Integer getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
	public String getCategoryName() {
		return categoryName;
	}
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	
	
}



//書本檔案配置(Book.hbm.xml)
<hibernate-mapping>
            <class name="com.zking.five.entity.Book" table="t_hibernate_book">
                  <id name="bookId" type="java.lang.Integer" column="book_id ">
                        <generator class="increment"></generator>
                  </id>
                  <property name="bookName" type="java.lang.String" column="book_name"/>
                  <property name="price" type="java.lang.Float" column="price"/>
                  
                  <set name="category" table="t_hibernate_book_category">
                       <key column="bid"></key>
                       <many-to-many class="com.zking.five.entity.Category" column="cid"></many-to-many>
                  </set>
            </class>
    </hibernate-mapping>



//書本類別檔案配置(Category.hbm.xml)
<hibernate-mapping>
            <class name="com.zking.five.entity.Category" table="t_hibernate_category">
                  <id name="categoryId" type="java.lang.Integer" column="category_id ">
                        <generator class="increment"></generator>
                  </id>
                 <property name="categoryName" type="java.lang.String" column="category_name"></property> 
                 name:指的是當前對映實體的屬性  table:對應的是中間表關聯關係(中間表的資料)交於對反管理

                 <set name="book" table="t_hibernate_book_category">
                 <!-- 指的是中間表的欄位(與當前對映實體對應對應的表的主鍵相關聯)-->
                    <key column="cid"></key>
                    <!--  class:多方的全類名,column:中間表字段(與多方主鍵的欄位)
                    -->
                    <many-to-many class="com.zking.five.entity.Book" column="bid"></many-to-many>
                 </set>
            </class>
    </hibernate-mapping>
  1. 多對多關係注意事項
    3.1 一定要定義一個主控方
    3.2 多對多刪除
    3.2.1 主控方直接刪除
    3.2.2 被控方先通過主控方解除多對多關係,再刪除被控方
    3.2.3 禁用級聯刪除
    3.3 關聯關係編輯,不需要直接操作橋接表,hibernate的主控方會自動維護