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会字段关联桥表以及关联表查询出关联对象
    
  2. hibernate的多对多
    2.1 hibernate可以直接映射多对多关联关系(看作两个一对多)

这里先定义一个book类

package com.zking.five.entity;

import java.util.HashSet;
import java.util.Set;

public class Book {
	private Integer bookId;
	private String bookName;
	private Float price;
	private Set<Category> categories = new HashSet<>();
	
	private Integer initCategories = 0;
	
	public Integer getInitCategories() {
		return initCategories;
	}
	public void setInitCategories(Integer initCategories) {
		this.initCategories = initCategories;
	}
	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;
	}
	public Set<Category> getCategories() {
		return categories;
	}
	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}
	
}



这里定义个Category 类

package com.zking.five.entity;

import java.util.HashSet;
import java.util.Set;

public class Category {
//	create table t_hibernate_category
//	(
//	   category_id int primary key auto_increment,
//	   category_name varchar(50) not null
//	);
	private Integer categoryId;
	private String categoryName;
	
	private Set<Book> books = new HashSet<>();
	private Integer initBooks = 0;
	

	public Integer getInitBooks() {
		return initBooks;
	}

	public void setInitBooks(Integer initBooks) {
		this.initBooks = initBooks;
	}

	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;
	}

	public Set<Book> getBooks() {
		return books;
	}

	public void setBooks(Set<Book> books) {
		this.books = books;
	}
	
	
}

  1. 多对多关系注意事项
    3.1 一定要定义一个主控方
    3.2 多对多删除
    3.2.1 主控方直接删除
    3.2.2 被控方先通过主控方解除多对多关系,再删除被控方
    3.2.3 禁用级联删除
    3.3 关联关系编辑,不需要直接操作桥接表,hibernate的主控方会自动维护

这个是Category配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-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">
    <!-- class指的表的主键对应生成的类,大白话就是id自增长 -->
       <generator class="increment"></generator>
    </id>
    <property name="categoryName" type="java.lang.String" column="category_name"></property>
    <!-- 会报错 -->
  <!-- inverse:为false是主控方 -->
     <set table="t_hibernate_book_category" name="books" cascade="save-update" inverse="false">
    <key column="cid"></key>
    <many-to-many class="com.zking.five.entity.Book" column="bid"/>
    </set>
     
  </class>
</hibernate-mapping>

这个是book配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class table="t_hibernate_book" name="com.zking.five.entity.Book">

    <id name="bookId" type="java.lang.Integer" column="book_id">
    <!-- class指的表的主键对应生成的类,大白话就是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>
    
    
    <!-- inverse:为true是被控方 -->
    <set table="t_hibernate_book_category" name="categories" cascade="save-update" inverse="true">
    <key column="bid"></key>
    <many-to-many class="com.zking.five.entity.Category" column="cid"/>
    </set>
     
  </class>
</hibernate-mapping>

如下是bookdao类的新增方法:

public Integer save(Book book) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		Integer b =  (Integer) session.save(book);
		transaction.commit();
		session.close();
		return b;
	}

如下是Categorydao类的新增方法:

 public Integer save(Category category) {
    	Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		Integer cid = (Integer) session.save(category);
		transaction.commit();
		session.close();
		return cid;
    }

测试类:

 * book.hbm:fasle
	 * category.hbm:true
	 * 代表的意思是:将关系维护的责任交给book
	 * 
	 * 注意
	 * hibernate通过管理持久对象来操作数据库
	 */
	@Test
	public void testAdd() {
		Book book = new Book();
		book.setBookName("肾虚");
		book.setPrice(55f);
		Category category = new Category();
		category.setCategoryId(3);
		Category c = this.CategoryDao.get(category);
		book.getCategories().add(c);
		this.BookDao.save(book);
	}
	@Test
	public void testAdd2() {
		Book book = new Book();
		book.setBookId(5);
		Category category = new Category();
		category.setCategoryName("玄幻");
		category.getBooks().add(this.BookDao.get(book));
		this.CategoryDao.save(category);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值