迫切内连接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. Oracle ebs 数据脱敏

    https://blog.csdn.net/pan_tian/article/details/16120351Data Masking可对数据进行不可逆的去身份化后,再用于非生产环境,同时自动保留引用 ...

  2. C# WebSocket Fleck 调用非托管C++ DLL 实现通信(使用stringbuilder接收)

     [DllImport(@"XXX.dll", CallingConvention = CallingConvention.StdCall)]public static exter ...

  3. AbstractRoutingDataSource 实现动态切换数据源

    扩展AbstractRoutingDataSource类 package com.datasource.test.util.database; import org.springframework.j ...

  4. 16bit CRC算法C语言实现

    #define CRC_16_POLYNOMIALS 0x8005 unsigned short CRC16_3(unsigned char* pchMsg, unsigned short wData ...

  5. Java开发微服务为什么一定要选spring cloud?

    来自:网易乐得技术团队,作者:董添 李秉谦 现如今微服务架构十分流行,而采用微服务构建系统也会带来更清晰的业务划分和可扩展性.同时,支持微服务的技术栈也是多种多样的,本系列文章主要介绍这些技术中的翘楚 ...

  6. NAT详解:基本原理、穿越技术(P2P打洞)、端口老化等

    这是一篇介绍NAT技术要点的精华文章,来自华3通信官方资料库,文中对NAT技术原理的介绍很全面也很权威,对网络应用的应用层开发人员而言有很高的参考价值. 学习交流 移动端即时通讯学习交流: 21589 ...

  7. Android JNI 学习(九):Static Fields Api & Static Methods Api

    一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...

  8. 每天学点SpringCloud(五):如何使用高可用的Eureka

    前几篇文章我们讲了一下Eureka的基础使用,但是呢有一个很重要的问题,我们讲的都是单机版的情况,如果这个时候Eureka服务挂了的话,那么我们的服务提供者跟服务消费者岂不是都废了?服务提供者和消费者 ...

  9. Windows 系统中的 CMD 黑窗口简单介绍

    简介 DOS是磁盘操作系统的缩写,是个人计算机上的一类操作系统DOS命令,是DOS操作系统的命令,是一种面向磁盘的操作命令,主要包括目录操作类命令.磁盘操作类命令.文件操作类命令和其它命令.DOS系统 ...

  10. 通过 Emoji 表情标识 Git 每次提交的信息

    在 Git 提交时我们可以通过附加 emoji 表情来着重提醒其他开发人员本次地更改重点,也就是说我们可通过不同的表情可以快速地知道这次提交具体做了哪方面地工作. 比如说本次提交我添加了一个新的单元测 ...