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