Hibernate(十三)迫切内连接fetch
迫切内连接fetch
内连接和迫切内连接的区别:
其主要区别就在于封装数据,因为他们查询的结果集都是一样的,生成底层的SQL语句也是一样的。
1.内连接:发送就是内连接的语句,封装的时候将属于各自对象的数据封装到各自的对象中,最后得到一个List<Object[]>。
2.迫切内连接:发送的是内连接的语句,需要在编写HQL的时候再join后添加一个fetch关键字,Hibernate会发送HQL中的fetch关键字,从而将每条数据封装到对象中,最后得到一个List<Customer>。
但是,迫切内连接封装以后会出现重复的数据,因为假设我们查询到目前有三条记录,就会被封装到三个对象中,其实我们真正的用户对象只有两个,所以往往自己在手动编写迫切内连接的时候会使用distinct去掉重复值。
普通内连接,就是将用户customer,和关系的订单ods,分成两个object对象返回。
/*** 普通内连接*/@Testpublic void fun(){Session session = HibernateUtils.getSession();Transaction tx = session.beginTransaction();List<Object[]> list = session.createQuery("from Customer cst inner join cst.ods").list();for (Object[] objects : list) {System.out.println(Arrays.toString(objects));}tx.commit();session.close();}
[Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@133e019b, deep.domain.Order@41382722]], deep.domain.Order@133e019b]
[Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@133e019b, deep.domain.Order@41382722]], deep.domain.Order@41382722]
而迫切连接就是将用户customer中的订单ods封装进customer中的ods属性中,一起返回
/*** 迫切内连接*/@Testpublic void fun1(){Session session = HibernateUtils.getSession();Transaction tx = session.beginTransaction();List<Customer> list = session.createQuery("from Customer cst inner join fetch cst.ods").list();for(Customer cst : list){System.out.println(cst);}tx.commit();session.close();}
Hibernate:
select
customer0_.cust_id as cust_id1_0_0_,
ods1_.order_id as order_id1_3_1_,
customer0_.cust_name as cust_nam2_0_0_,
customer0_.cust_gender as cust_gen3_0_0_,
customer0_.cust_age as cust_age4_0_0_,
customer0_.cust_phone as cust_pho5_0_0_,
ods1_.detail_id as detail_i2_3_1_,
ods1_.cust_order_id as cust_ord3_3_1_,
ods1_.cust_order_id as cust_ord3_3_0__,
ods1_.order_id as order_id1_3_0__
from
customera customer0_
inner join
ordersa ods1_
on customer0_.cust_id=ods1_.cust_order_id
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@c41709a, deep.domain.Order@7db0565c]]
Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@c41709a, deep.domain.Order@7db0565c]]
Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@52eacb4b, deep.domain.Order@5528a42c]]
Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@52eacb4b, deep.domain.Order@5528a42c]]
Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@1edb61b1, deep.domain.Order@1a6f5124]]
Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@1edb61b1, deep.domain.Order@1a6f5124]]
Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@29539e36, deep.domain.Order@cc62a3b]]
Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@29539e36, deep.domain.Order@cc62a3b]]
转载于:https://www.cnblogs.com/deepSleeping/p/10012658.html
Hibernate(十三)迫切内连接fetch相关推荐
- Hibernate【查询、连接池、逆向工程】
2019独角兽企业重金招聘Python工程师标准>>> 前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式....到目前为止,我们都是使用一些简单的主 ...
- Hibernate进阶之如何使用Hql内连接,外连接以及自连接
一.sql语句中的 内连接.自连接和外连接: 1.使用等值连接/内连接查询,查询客户姓名,订单编号,订单价格 等值连接/内连接:只能查询出符合条件的记录: select c.name,o.ordern ...
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等...
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- SQL 左外连接,右外连接,全连接,内连接带图详细介绍
SELECT id, name,description,img_url,sort,is_display from bbs_brand ORDER BY id DESC limit startR ...
- Hibernate提供的内置标识符生成器
Hibernate提供的内置标识符生成器 Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键来识别或区分同一个表的不同记录.Hibernate使用OID(对象标识符)来统一两者之 ...
- Hibernate多对多映射 - 连接表
Hibernate多对多映射 - 连接表 今天我们将使用XML和注释配置来研究Hibernate多对多映射.之前我们看过如何在Hibernate中实现One To One和One To Many映射. ...
- 实时双频Wi-Fi如何实现下一代车内连接
实时双频Wi-Fi如何实现下一代车内连接 How real simultaneous dual band Wi-Fi enables next-generation in-vehicle connec ...
- MySQL 学习笔记(15)— 连接查询(内连接、左外连接、右外连接、全外连接、交叉连接、自然连接等)
本文参考:https://gitbook.cn/gitchat/column/undefined/topic/5db92b68a9c3a53bc3800eff SQL 支持的连接查询包括内连接.外连接 ...
- mysql内连接运算量会增加多少_新年手打,40道经典MYSQL面试干货,速来收藏
MySQL 面试题 1.MySQL 中有哪几种锁? 1.表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最 高,并发度最低. 2.行级锁:开销大,加锁慢:会出现死锁:锁定粒度最小,发 ...
最新文章
- 那些年,我们一起追过的恺明大神!
- 基于Redis实现分布式应用限流--转
- 独立开发者:新手做2D手游该用哪些工具?
- python抖音github_GitHub - eternal-flame-AD/Douyin-Bot: Python 抖音机器人,论如何在抖音上找到漂亮小姐姐?...
- 迅雷BT下载电影99.9%不动原地循环解决方案
- java 一年 周数_Java获取一年有多少周、某周的第一天和最后一天,当前时间所在当年的实际周数...
- 互联网公司招聘--今日头条--产品经理-2017年笔试题1
- 广告机-开机自动播放-视频-电影-图片-竖屏-分屏-展示机
- 如何一条命令查询笔记本电池损耗情况-生成报告
- 01_CCC3.0数字钥匙_蓝牙OOB配对过程
- 简析JavaScript 事件绑定、事件冒泡、事件捕获和事件执行顺序
- 3 树莓派使用网线连接路由器获取网络
- 【MockJS】使用MockJS模拟数据 (超级详细)
- 常用的15个国外网站
- jvm设置http代理
- SCI入门级知识(带你了解SCI期刊)
- 仓储委外加工/周转加工
- JVM监控及诊断工具-GUI
- 圆弧防线用计算机怎么算,一种圆弧形放线工具的制作方法
- 新零售发展蓝海|全球无人零售货柜与无人便利店趋势兴起