hibernate:多对多关系查询
数据库的多对多:
数据库中不能直接映射多对多
处理:创建一个桥接表(中间表),将一个多对多关系转换成两个一对多
注:数据库多表联接查询,永远就是二个表的联接查询
hibernate的多对多:
hibernate可以直接映射多对多关联关系(看作两个一对多)
多对多关系注意事项:一定要定义一个主控方
案例:
书本表:t_hibernate_book
书本类型表:t_hibernate_category
关联表:t_hibernate_book_category
表结构:
book表:
category表:
关联表:
1.书本:Book.java
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> categorys=new HashSet<Category>();
private Integer initCagetorys=0;
public Integer getInitCagetorys() {
return initCagetorys;
}
public void setInitCagetorys(Integer initCagetorys) {
this.initCagetorys = initCagetorys;
}
public Set<Category> getCategorys() {
return categorys;
}
public void setCategorys(Set<Category> categorys) {
this.categorys = categorys;
}
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;
}
}
2.书本类型:category.java
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> books=new HashSet<Book>();
//强制立即加载
private Integer initBooks=0;
public Integer getInitBooks() {
return initBooks;
}
public void setInitBooks(Integer initBooks) {
this.initBooks = initBooks;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
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;
}
}
3.book.hbm.xml
<?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 ">
<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>
<!-- table:中间连接表
name="categorys":Book实体类里的类属性
-->
<set table="t_hibernate_book_category" name="categorys" cascade="save-update" inverse="false">
<!-- one -->
<key column="bid"></key>
<!-- many -->
<many-to-many column="cid" class="com.zking.five.entity.Category"></many-to-many>
</set>
</class>
</hibernate-mapping>
4.category.hbm.xml
<?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 ">
<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">
<key column="cid"></key>
<many-to-many column="bid" class="com.zking.five.entity.Book"></many-to-many>
</set>
</class>
</hibernate-mapping>
5.dao方法测试:
package com.zking.five.dao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zking.five.entity.Book;
import com.zking.two.util.SessionFactoryUtils;
public class BookDao {
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.getInitCagetorys())) {
Hibernate.initialize(b.getCategorys());
}
transaction.commit();
session.close();
return b;
}
}
package com.zking.five.dao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zking.five.entity.Category;
import com.zking.two.util.SessionFactoryUtils;
public class CategoryDao {
public Category getCategory(Category category) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Category c = session.get(Category.class, category.getCategoryId());
if(c!=null&&new Integer(1).equals(category.getInitBooks())) {
Hibernate.initialize(c.getBooks());
}
transaction.commit();
session.close();
return c;
}
}
6.测试类:
package com.zking.five.dao;
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 Book book=new Book();
private CategoryDao categoryDao=new CategoryDao();
private Category category=new Category();
/**
* 通过一本书能够查询到多个类别
* jdbc:三表联查
* hibernate:只需要查询单个对象即可,它会自动关联查询,交给映射文件即可
*/
@Test
public void testGet() {
book.setBookId(4);
book.setInitCagetorys(1);
Book b = this.bookDao.get(book);
System.out.println(b.getBookName());
for (Category ba : b.getCategorys()) {
System.out.println(ba.getCategoryName());
}
}
/**
* 通过一个列查到多本书
*/
@Test
public void testGetCategory() {
category.setCategoryId(1);
category.setInitBooks(1);
Category c = this.categoryDao.getCategory(category);
System.out.println(c.getCategoryName());
for (Book b : c.getBooks()) {
System.out.println(b.getBookName());
}
}
}
结果展示: