多对多关系原理

多对多关系
查询一本书
session.get(Book.class,5)->
select * from t_hibernate_book where book_id =?(1)
resultSet->1 西游记 50
建模 Book b=Class.forName(“com.zking.five.entity.Book”).newInstance();
b.setBookId(1);
b.setBookName(西游记);
b.setPrice(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->6	1	1     (1,2)
           7	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
1	西游记	50
2	红楼梦	50
Category c=Class.forName("com.zking.five.entity.Category").newInstance();

原理参照EntityBaseDao->List categoeies
5.b.setcategories(categories);

–>

一对多关系

级联查询原理:
session.get(Order.class,1)
select * from t_hibernate_order where orderId=?(1)
1 p3
Order o= class.forName(“com.zking.four.entity.Order”).newInstance();
o.setOrderId(1);
o.setOrderNo(p3);

通过one-to-many中的class属性找到对应的映射文件
select * from t_hibernate_order_item where oid=?(3)
6 6 6 3
7 4 4 3
8 1 1 3
9 3 3 3
10 2 2 3
11 5 5 3
ResultSet rs
com.zking.four.entity.Order
List 原理参照mvc中EntityBaseDao,最终获取到有值的list->orderItems
o.setOrderItems(orderItems);
–>

猜你喜欢

转载自blog.csdn.net/qq_43181514/article/details/83478627