1. 程式人生 > >hibernate的多對多級聯查詢

hibernate的多對多級聯查詢

  1. 資料庫的多對多 1.1 資料庫中不能直接對映多對多 處理:建立一個橋接表(中間表),將一個多對多關係轉換成兩個一對多 注2:交叉連線 注3:外連線:left(左)/right(右)/full(左右) 主從表:連線條件不成立時,主表記錄永遠保留,與null匹配

  2. hibernate的多對多 2.1 hibernate可以直接對映多對多關聯關係(看作兩個一對多)

  3. 多對多關係注意事項 3.1 一定要定義一個主控方 3.2 多對多刪除 3.2.1 主控方直接刪除 3.2.2 被控方先通過主控方解除多對多關係,再刪除被控方 3.2.3 禁用級聯刪除 3.3 關聯關係編輯,不需要直接操作橋接表,hibernate的主控方會自動維護

1.多對多查詢建立主表xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	<hibernate-mapping>
	<!-- 
		table:實體類所對應表
		name:實體類的全類名
	 -->
		<class  table="t_hibernate_book" name="com.zking.five.entity.Book">
		<!-- 
			name:實體類的屬性
			type:實體類的屬性型別
			column:指的是資料庫表的主鍵列
		 -->
			<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>
			<property name="price" type="java.lang.Float" column="price"></property>
			
			<!-- 
				order-by:指的是資料庫中的表字段
				table:橋接表名
			 -->
			<set table="t_hibernate_book_category" name="Categories" cascade="save-update" inverse="false">
			<!-- 
				bid:指的是在橋接表中的book表外來鍵
				cid:指的是在橋接表中的Category表外來鍵
			 -->
				<key column="bid"></key>
				<many-to-many class="com.zking.five.entity.Category" column="cid"></many-to-many>
			</set>
			
		</class>
	</hibernate-mapping>

1.多對多查詢建立從表xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	<hibernate-mapping>
	
		<class  table="t_hibernate_category" name="com.zking.five.entity.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>
			<set table="t_hibernate_book_category" name="books" cascade="save-update" inverse="true">
			<!-- 
				cid:指的是在橋接表中的Category表外來鍵
				bid:指的是在橋接表中的book表外來鍵
			 -->
				<key column="cid"></key>
				<many-to-many class="com.zking.five.entity.Book" column="bid"></many-to-many>
			</set>
			
		</class>
	</hibernate-mapping>

程式碼解說多對多查詢


import static org.junit.Assert.*;

import org.junit.Test;

import com.zking.five.entity.Book;
import com.zking.five.entity.Category;

public class BookDaoTest {
	private BookDao bookdao=new BookDao();
	private CategoryDao categorydao=new CategoryDao();
//	hiberbate:自需要查詢單個物件即可,它會自動關聯查詢,交給對映檔案即可
	@Test
	public void testGet() {
		Book book=new Book();
		book.setBookId(4);
		book.setInitCategories(1);
		Book b = this.bookdao.get(book);
		System.out.println(b.getBookName());
		
	}
	
	
	@Test
	public void testGet1() {
		Category category=new Category();
		category.setCategoryId(2);;
		category.setInibooks(1);;
		Category c = this.categorydao.get(category);
		System.out.println(c.getCategoryName());
		for (Book b:c.getBooks()) {
			System.out.println(b.getBookName());
		}
	}

//dao方法
public Book get(Book book) {
		Session session = SessionFactoryUtils.getSession();
		Transaction transaction = session.beginTransaction();
		Book b = session.get(Book.class, book.getBookId());
		if(b!=null && new Integer(1).equals(book.getInitCategories())) {
			Hibernate.initialize(b.getCategories());
		}
		transaction.commit();
		session.close();
		return b;
	}