数据库的多对多 1.1 数据库中不能直接映射多对多 处理:创建一个桥接表(中间表),将一个多对多关系转换成两个一对多
注1:数据库多表联接查询 永远就是二个表的联接查询
A B C D t1 C t2 D t3 A B AB select * from A,B,AB WHERE A.aID=AB.aID and b.bid = AB.bid where 在hibernate中,你只管查询当前表对象即可, hibernate会字段关联桥表以及关联表查询出关联对象这是多对多的一个例子
查询一本书为例 session .get(Book.class, 1) select * from t_hibernate_book where book_id = ?(1) resultSet -> 1 西游记 50 Book b = class.forname ("com.zking.five.entity.Book").new Instance(); b.setBook_id(1); b.setBook_name(西游记); b.getPrice(50); hibernate 处理关联关系 1、通过 set 标签 找到桥接表(table 属性), 2、找到当前实体类对应表的主键在桥接表中的外键(key 标签中的column属性中定义) 3、查出关联表 (t_hibernate_category)中的主键(category_id ) select cid from t_hibernate_book_category where bid = ?(1) resultSet -> 关注最后的一列 8 1 1 9 1 2 list<String > 1/ 2 4、查出来的外键关联的实体类(class = com.zking.five.entity.Category),它可以与 这个类的映射文件(class 标签中name =com.zking.five.entity.Category),从而 找到了对应的实体类对应的表的主键id标签中的colum字段 -》category_id select * from t_hibernate_category where category_id = ?(1) select * from t_hibernate_category where category_id = ?(2) list<String > 1 古典 2 神话 class.forname ("com.zking.five.entity.Category").new Instance(); 参照EntityBaseDao -> list<c> categories 5.b.setCategories(categories);#关键点都在数据库中的外键上面,请好好理解下面这二句SQL和一对多及多对一的关系 #select * from Orders where cid=? //这条SQL返回客户对应的0-n个订单 #select * from Customers where customerId=? //这条SQL返回订单对应的1个客户 #通过这二条SQL不难看出,外键在这里有二个用途:查找客户的订单,查找订单对应的客户 2. 案例:如何建立客户和订单一对多双向关联 2.1 先不建立客户和订单的关联关系,定义实体及映射文件,单独执行保存操作 2.2 建立客户到订单的一对多关联关系
2.3 建立订单到客户的多对一关联关系
<!--1.注释 2.只读--> <property name="cid" type="java.lang.Integer" column="cid" insert="false" update="false"> </property>2.4 注意:在Hibernate当中定义实体对象的集合属性时,只能使用接口而不能使用类
ps :这是多对多的关联查询 实体类的代码: 1.这是书籍的代码:
public class Book { private Integer bookId; private String bookName; private Float price ; private Set<Category> categories = new HashSet<>(); private Integer initCategorys = 0; public Integer getInitCategorys() { return initCategorys; } public void setInitCategorys(Integer initCategorys) { this.initCategorys = initCategorys; } public Set<Category> getCategories() { return categories; } public void setCategories(Set<Category> categories) { this.categories = categories; } 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 class Category { 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 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; } }这是配置实体类的xml文件
配置书籍的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> <set table="t_hibernate_book_category" name="categories" cascade="save-update" inverse="false"> <!-- one --> <key column="bid"></key> <!-- many 一个书对应多个类型 --> <many-to-many column="cid" class="com.zking.five.entity.Category" /> </set> </class> </hibernate-mapping>配置书籍类型的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="delete" inverse="true"> <!-- one --> <key column="cid"></key> <!-- many 一个类别对应多本书--> <many-to-many column="bid" class="com.zking.five.entity.Book"/> </set> </class> </hibernate-mapping> 注意:写完以后不要忘了 在主配置xml加上 实体类相对应的xmldao方法
//这是书籍的查询 public Book get(Book book) { Configuration configure = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configure.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Book b = session.get(Book.class, book.getBookId()); if (b != null && new Integer(1).equals(book.getInitCategorys())) { Hibernate.initialize(b.getCategories()); } transaction.commit(); session.close(); return b; } //这是书籍类型的查询方法 public Category get(Category category) { Configuration configure = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configure.buildSessionFactory(); Session session = sessionFactory.openSession(); 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; }