迫切内连接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的更多相关文章

  1. [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Hibernate迫切左外连接和迫切内连接

    •迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee  ...

  3. Hibernate【查询、连接池、逆向工程】

    前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式....到目前为止,我们都是使用一些简单的主键查询阿...使用HQL查询所有的数据....本博文主要讲解Hiberna ...

  4. Hibernate多表查询连接操作

    SQL多表操作分类; 1.交叉连接:select*from t_customer cross Join t_order; 2.显示内连接: select*from t_customer c inner ...

  5. HOL的多表查询——内连接、外连接

    1.内连接: 由于学生和班级是多对一的关系,班级对应学生是一对多的关系,因此,需要先对学生和班级进行配置. (1)创建Student类(多的一方): package pers.zhb.domain; ...

  6. HQL的内连接查询

    /** * HQL的内连接查询 * String hql="from Customer c inner join fetch c.linkmans"; */ @Test publi ...

  7. [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. Hibernate整合C3P0实现连接池

    Hibernate整合C3P0实现连接池 hibernate中可以使用默认的连接池,无论功能与性能都不如C3PO(网友反映,我没有测试过),C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI ...

  9. Hibernate提供的内置标识符生成器

    Hibernate提供的内置标识符生成器 Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键来识别或区分同一个表的不同记录.Hibernate使用OID(对象标识符)来统一两者之 ...

随机推荐

  1. Linux下Oracle数据库的安装

    记录详细过程以备使用 一.准备安装 为了确保Oracle数据库11g能够成功安装,您需要做好准备工作,例如检查网络配置.更改Linux内核参数.创建用户Oracle.创建安装目录.设置用户Oracle ...

  2. SVN设置全局忽略提交文件或者目录

    在目录中右击选择TortoiseSVN -> 设置 -> 常规设置 -> 全局忽略样式 修改全局忽略样式(可以设置忽略的文件或者目录,以空格隔开):*.o *.lo *.la *.a ...

  3. 每天学点SpringCloud(十二):Zipkin全链路监控

    Zipkin是SpringCloud官方推荐的一款分布式链路监控的组件,使用它我们可以得知每一个请求所经过的节点以及耗时等信息,并且它对代码无任何侵入,我们先来看一下Zipkin给我们提供的UI界面都 ...

  4. 应用监控CAT之cat-consumer源码阅读(二)

    之前讲了 cat-client 进行cat埋点上报,那么上报给谁呢?以及后续故事如何?让我们来看看 cat-consumer 是如何接收处理的? 由cat-client发送数据,cat-consume ...

  5. 用excel记录测试bug问题总结

    前几天与开发在讨论问题的时候,开发提了一个问题,说是已经解决的问题,能否用excel表格总结一下,问了一下原因,感觉想法很好,就总结了一下. 在上家公司的时候,提交bug用的是mantis,现在是禅道 ...

  6. Redis学习笔记之Redis基本数据结构

    Redis基础数据结构 Redis有5种基本数据结构:String(字符串).list(列表).set(集合).hash(哈希).zset(有序集合) 字符串string 字符串类型是Redis的va ...

  7. 关于在运行java连接MongoDB时遇到的连接超时问题

    在linux虚拟机里运行创建集合的时候,遇到了以下问题: 这里我最开始时使用的虚拟机IP地址进行连接,因此就出现了错误,连接超时. 在网上查了一些类似的问题,说要改驱动,后来才发现不是这个问题. 在本 ...

  8. Linux 下 pushd,popd,cd- 用法

    一,为何要使用这几个命令? 可能大家会有疑问,为何要使用这几个命令,   难道用cd不就可以切换目录了吗?   没错,使用cd就可以切换到需要访问的目录,   但是有时会是一个路径很长,层次很多的目录 ...

  9. Python - 使用objgraph生成对象引用关系图

    1- objgraph简介 HomePage:https://mg.pov.lt/objgraph/ PyPI:https://pypi.org/project/objgraph/ 一般用于分析pyt ...

  10. CMake设置编译参数

    项目中的CMake编译参数一直参照Muduo进行设置. Muduo的CMakeLists.txt中,MAKE_CXX_FLAGS设置较为清晰明了,因此一直在项目中沿用. set(CXX_FLAGS - ...