Hibernate(十三)迫切内连接fetch
迫切内连接fetch
内连接和迫切内连接的区别:
其主要区别就在于封装数据,因为他们查询的结果集都是一样的,生成底层的SQL语句也是一样的。
1.内连接:发送就是内连接的语句,封装的时候将属于各自对象的数据封装到各自的对象中,最后得到一个List<Object[]>。
2.迫切内连接:发送的是内连接的语句,需要在编写HQL的时候再join后添加一个fetch关键字,Hibernate会发送HQL中的fetch关键字,从而将每条数据封装到对象中,最后得到一个List<Customer>。
但是,迫切内连接封装以后会出现重复的数据,因为假设我们查询到目前有三条记录,就会被封装到三个对象中,其实我们真正的用户对象只有两个,所以往往自己在手动编写迫切内连接的时候会使用distinct去掉重复值。
普通内连接,就是将用户customer,和关系的订单ods,分成两个object对象返回。
/**
* 普通内连接
*/
@Test
public 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属性中,一起返回
/**
* 迫切内连接
*/
@Test
public 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]]
Hibernate(十三)迫切内连接fetch的更多相关文章
- [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate迫切左外连接和迫切内连接
•迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee ...
- Hibernate【查询、连接池、逆向工程】
前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式....到目前为止,我们都是使用一些简单的主键查询阿...使用HQL查询所有的数据....本博文主要讲解Hiberna ...
- Hibernate多表查询连接操作
SQL多表操作分类; 1.交叉连接:select*from t_customer cross Join t_order; 2.显示内连接: select*from t_customer c inner ...
- HOL的多表查询——内连接、外连接
1.内连接: 由于学生和班级是多对一的关系,班级对应学生是一对多的关系,因此,需要先对学生和班级进行配置. (1)创建Student类(多的一方): package pers.zhb.domain; ...
- HQL的内连接查询
/** * HQL的内连接查询 * String hql="from Customer c inner join fetch c.linkmans"; */ @Test publi ...
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate整合C3P0实现连接池
Hibernate整合C3P0实现连接池 hibernate中可以使用默认的连接池,无论功能与性能都不如C3PO(网友反映,我没有测试过),C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI ...
- Hibernate提供的内置标识符生成器
Hibernate提供的内置标识符生成器 Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键来识别或区分同一个表的不同记录.Hibernate使用OID(对象标识符)来统一两者之 ...
随机推荐
- day_3各种数据类型与各种运算符
首先我们复习一下昨天的内容 1:语言的分类: --有三种 机器语言,汇编语言,高级语言 运行的效率是机器语言最高 开发效率 是高级语言最高 2:计算机由五大部分组成:控制器+运算器+存储器+inpu ...
- 大叔学ML第一:梯度下降
目录 原理 实践一:求\(y = x^2 - 4x + 1\)的最小值 实践二:求\(z = x^2 + y^2 + 5\)的最小值 问答时间 原理 梯度下降是一个很常见的通过迭代求解函数极值的方法, ...
- JDK设计模式之——责任链(Filter)
责任链的设计模式可以参考Servlet的FilterChain.FilterChain中的每个Filter(过滤器)就像一个个的链条 web开发中 有时候需要对接口request和response进行 ...
- Spring 通知和顾问进行增强
使用顾问增加前置增强和后置增强 <bean id="1" class="目标对象"></bean> <bean id=" ...
- [node.js] fs.renameSync()报错
初学node.js,跟着node入门,操作了一遍.在最后一步,上传图片并显示时遇到报错 fs.js: throw err; ^ Error: ENOENT: no such file or direc ...
- .Net Core新建解决方案,添加项目引用,使用VSCode调试
并不是我自己琢磨的,是看了别人学习的,因为写的都不完整,所以就整理一下记录后面忘了回看. 反正.Net Core是跨平台的,就不说在什么系统上了.假设我要建一个名为Doggie的解决方案,里面包含了一 ...
- sudo的使用和配置
1 sudo是什么 Sudo是Unix/Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权利”,让他们执行一些只有超级用户或其他特许用户才能完成的任务,比如:运行一些像 ...
- 如何一步步在生产环境上部署django和vue
本文由云+社区发表 本文主要讲述了如何一步步在生产环境上部署django和vue,操作系统默认为centos 说明:后文中出现的以下字符串均表示具体的路径或者名称,含义如下: DJANGO_DIR-- ...
- [转]Memcache的使用和协议分析详解
Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可以应 ...
- RabbitMQ系列(四)RabbitMQ事务和Confirm发送方消息确认——深入解读
RabbitMQ事务和Confirm发送方消息确认--深入解读 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器 ...