案例:
1:实体类:
package com.crud.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;
}
}
package com.crud.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;
}
}
2.配置文件:
<?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">
<!-- 多对多SQL形成过程:
session.get(Book.class,5);
以前的做法:select * from t_hibernate_book where bookId=?(5)
得到:resultSet->
值: 5 a 10
建模之后找到Book类实例:
Book book= Class.forName("com.crud.five.entity.Book").newInstance();
拿到属性对象: field 可以把resultlset里的属性全都设值进去
book.setBookId(5);
book.setBookName(a); 通过反射做的
book.setPrice(12);
categorys为什么有值?
1.建模之后,通过当前实体类的映射文件找到set标签中table属性
形成SQL语句:select * from t_hibernate_book_category
2.继续读取配置文件,拿到set标签中的子标签key的column属性(当前类对应的表主键在桥接表中的外键)
进一步生成SQL语句:select * from t_hibernate_book_category where bid=?(bookId=4)
sql语句之后,又形成一个:resultSet
1 古典
2 神话
EntityBaseDao中的executeQuery方法,对result进行处理,最终返回
-->
<hibernate-mapping>
<class table="t_hibernate_book" name="com.crud.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.crud.five.entity.Category"></many-to-many>
</set>
</class>
</hibernate-mapping>
<?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.crud.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.crud.five.entity.Book"></many-to-many>
</set>
</class>
</hibernate-mapping>
3.dao方法:
/**
* 增加书籍
* @param book
* @return
*/
public Integer add(Book book) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Integer s =(Integer) session.save(book);
transaction.commit();
session.close();
return s;
}
/**
* 增加书籍的同时,选好类别
*
* book.hbm.xml:inverse="false" 主控方:书籍
* category.hbm.xml:inverse="true" 被控方:书籍类别
*/
@Test
public void testAdd() {
book.setBookName("斗破苍穹11");
book.setPrice(23f);
category.setCategoryId(5);
book.getCategorys().add(this.categoryDao.getCategory(category));
this.bookDao.add(book);
}