[Hibernate] inner Join和 left Join
@Test
public void test11(){
Session ss=HibernateUtil.getSession();
//根据员工名称(SCOTT)找到和他所在的部门的其他员工的信息
String hql=" select e from Emp e inner join e.dept";//下面一条等效
//String hql="from Emp e inner join fetch e.dept";
Query qr=ss.createQuery(hql);
List<Emp> el =qr.list();
System.out.println(el.size());
for (Emp emp : el) {
System.out.println(emp);
}
HibernateUtil.closeResource(ss);
}
inner join代码
inner join输出27条结果,但是分块输出,条件查询也执行了几次.
Hibernate:
select
emp0_.empno as empno1_1_,
emp0_.ename as ename2_1_,
emp0_.job as job3_1_,
emp0_.mgr as mgr4_1_,
emp0_.hiredate as hiredate5_1_,
emp0_.sal as sal6_1_,
emp0_.comm as comm7_1_,
emp0_.deptno as deptno8_1_
from
EMP2 emp0_
inner join
DEPT2 dept1_
on emp0_.deptno=dept1_.deptno
24
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=7369, eName=SMITH, job=CLERK, mgr=7902, hireDate=1980-12-17 00:00:00.0, sal=800.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=7499, eName=ALLEN, job=SALESMAN, mgr=7698, hireDate=1981-02-20 00:00:00.0, sal=1600.0, comm=300.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7521, eName=WARD, job=SALESMAN, mgr=7698, hireDate=1981-02-22 00:00:00.0, sal=1250.0, comm=500.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7566, eName=JONES, job=MANAGER, mgr=7839, hireDate=1981-04-02 00:00:00.0, sal=2975.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7654, eName=MARTIN, job=SALESMAN, mgr=7698, hireDate=1981-09-28 00:00:00.0, sal=1250.0, comm=1400.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7698, eName=BLAKE, job=MANAGER, mgr=7839, hireDate=1981-05-01 00:00:00.0, sal=2850.0, comm=null, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=7782, eName=CLARK, job=MANAGER, mgr=7839, hireDate=1981-06-09 00:00:00.0, sal=2450.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YORK
]]
Emp [empNo=7788, eName=SCOTT, job=ANALYST, mgr=7566, hireDate=1987-04-19 00:00:00.0, sal=3000.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7839, eName=KING, job=PRESIDENT, mgr=null, hireDate=1981-11-17 00:00:00.0, sal=5000.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YOR
K]]
Emp [empNo=7844, eName=TURNER, job=SALESMAN, mgr=7698, hireDate=1981-09-08 00:00:00.0, sal=1500.0, comm=0.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7876, eName=ADAMS, job=CLERK, mgr=7788, hireDate=1987-05-23 00:00:00.0, sal=1100.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7900, eName=JAMES, job=CLERK, mgr=7698, hireDate=1981-12-03 00:00:00.0, sal=950.0, comm=null, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7902, eName=FORD, job=ANALYST, mgr=7566, hireDate=1981-12-03 00:00:00.0, sal=3000.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7934, eName=MILLER, job=CLERK, mgr=7782, hireDate=1982-01-23 00:00:00.0, sal=1300.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YORK]
]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=67, eName=瑞士队, job=MANAGER, mgr=2018, hireDate=2018-06-21 21:37:53.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=64, dName=WorldCup, loc=Moscow]]
Emp [empNo=19, eName=韩国队, job=MANAGER, mgr=7788, hireDate=2018-06-12 00:00:00.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=13, eName=InverseT, job=TEST, mgr=7788, hireDate=2018-06-28 11:16:49.0, sal=1000.0, comm=100.0, dept=Dept [deptNo=86, dName=HBM, loc=Diego]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=88, eName=Poly, job=Athlete, mgr=7788, hireDate=2018-06-28 10:21:47.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=90, dName=FOOTBALL, loc=Seattle]]
Emp [empNo=89, eName=Odin, job=Athlete, mgr=7788, hireDate=2018-06-28 10:21:47.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=90, dName=FOOTBALL, loc=Seattle]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=83, eName=SET, job=主播, mgr=7788, hireDate=2018-06-28 10:08:45.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=81, dName=BigDept, loc=NEWYORK]]
Emp [empNo=87, eName=InverseF, job=TEST, mgr=7788, hireDate=2018-06-28 10:18:08.0, sal=1000.0, comm=100.0, dept=Dept [deptNo=86, dName=HBM, loc=Diego]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=16, eName=TOM, job=CODER, mgr=7788, hireDate=2018-06-28 11:18:18.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=17, dName=VR, loc=Phoenix]]
Emp [empNo=63, eName=德国队, job=MANAGER, mgr=7788, hireDate=2018-06-21 21:24:37.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=64, dName=WorldCup, loc=Moscow]]
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
DEPT2 dept0_
where
dept0_.deptno=?
Emp [empNo=110, eName=Police, job=Police, mgr=110, hireDate=2001-09-09 09:46:40.0, sal=4000.0, comm=800.0, dept=Dept [deptNo=5, dName=DeptX, loc=null]]
inner Join输出结果
//left join
@Test
public void test12(){
Session ss=HibernateUtil.getSession();
//根据员工名称(SCOTT)找到和他所在的部门的其他员工的信息
//String hql="from Emp e left outer join e.dept on e.deptNo=e.dept.deptNo";//获取Emp+Dept的集合
//String hql="select e from Emp e left outer join e.dept on e.deptNo=e.dept.deptNo";//获取Emp
//以上两个个需要deptNo属性,在配置文件中配置property和Emp类中生成相应的getter setter
String hql="from Emp e left outer join fetch e.dept ";//获取Emp
Query qr=ss.createQuery(hql); List<Emp> el =qr.list();
System.out.println(el.size());
for (Emp emp : el) {
System.out.println(emp);
}
HibernateUtil.closeResource(ss);
}
left join
Hibernate:
select
emp0_.empno as empno1_1_0_,
dept1_.deptno as deptno1_0_1_,
emp0_.ename as ename2_1_0_,
emp0_.job as job3_1_0_,
emp0_.mgr as mgr4_1_0_,
emp0_.hiredate as hiredate5_1_0_,
emp0_.sal as sal6_1_0_,
emp0_.comm as comm7_1_0_,
emp0_.deptno as deptno8_1_0_,
dept1_.dname as dname2_0_1_,
dept1_.loc as loc3_0_1_
from
EMP2 emp0_
left outer join
DEPT2 dept1_
on emp0_.deptno=dept1_.deptno
27
Emp [empNo=7934, eName=MILLER, job=CLERK, mgr=7782, hireDate=1982-01-23 00:00:00.0, sal=1300.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YORK]
]
Emp [empNo=7839, eName=KING, job=PRESIDENT, mgr=null, hireDate=1981-11-17 00:00:00.0, sal=5000.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YOR
K]]
Emp [empNo=7782, eName=CLARK, job=MANAGER, mgr=7839, hireDate=1981-06-09 00:00:00.0, sal=2450.0, comm=null, dept=Dept [deptNo=10, dName=ACCOUNTING, loc=NEW YORK
]]
Emp [empNo=19, eName=韩国队, job=MANAGER, mgr=7788, hireDate=2018-06-12 00:00:00.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7902, eName=FORD, job=ANALYST, mgr=7566, hireDate=1981-12-03 00:00:00.0, sal=3000.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7876, eName=ADAMS, job=CLERK, mgr=7788, hireDate=1987-05-23 00:00:00.0, sal=1100.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7788, eName=SCOTT, job=ANALYST, mgr=7566, hireDate=1987-04-19 00:00:00.0, sal=3000.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7566, eName=JONES, job=MANAGER, mgr=7839, hireDate=1981-04-02 00:00:00.0, sal=2975.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7369, eName=SMITH, job=CLERK, mgr=7902, hireDate=1980-12-17 00:00:00.0, sal=800.0, comm=null, dept=Dept [deptNo=20, dName=RESEARCH, loc=DALLAS]]
Emp [empNo=7900, eName=JAMES, job=CLERK, mgr=7698, hireDate=1981-12-03 00:00:00.0, sal=950.0, comm=null, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7844, eName=TURNER, job=SALESMAN, mgr=7698, hireDate=1981-09-08 00:00:00.0, sal=1500.0, comm=0.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7698, eName=BLAKE, job=MANAGER, mgr=7839, hireDate=1981-05-01 00:00:00.0, sal=2850.0, comm=null, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7654, eName=MARTIN, job=SALESMAN, mgr=7698, hireDate=1981-09-28 00:00:00.0, sal=1250.0, comm=1400.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7521, eName=WARD, job=SALESMAN, mgr=7698, hireDate=1981-02-22 00:00:00.0, sal=1250.0, comm=500.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=7499, eName=ALLEN, job=SALESMAN, mgr=7698, hireDate=1981-02-20 00:00:00.0, sal=1600.0, comm=300.0, dept=Dept [deptNo=30, dName=SALES, loc=CHICAGO]]
Emp [empNo=110, eName=Police, job=Police, mgr=110, hireDate=2001-09-09 09:46:40.0, sal=4000.0, comm=800.0, dept=Dept [deptNo=5, dName=DeptX, loc=null]]
Emp [empNo=89, eName=Odin, job=Athlete, mgr=7788, hireDate=2018-06-28 10:21:47.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=90, dName=FOOTBALL, loc=Seattle]]
Emp [empNo=88, eName=Poly, job=Athlete, mgr=7788, hireDate=2018-06-28 10:21:47.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=90, dName=FOOTBALL, loc=Seattle]]
Emp [empNo=87, eName=InverseF, job=TEST, mgr=7788, hireDate=2018-06-28 10:18:08.0, sal=1000.0, comm=100.0, dept=Dept [deptNo=86, dName=HBM, loc=Diego]]
Emp [empNo=13, eName=InverseT, job=TEST, mgr=7788, hireDate=2018-06-28 11:16:49.0, sal=1000.0, comm=100.0, dept=Dept [deptNo=86, dName=HBM, loc=Diego]]
Emp [empNo=63, eName=德国队, job=MANAGER, mgr=7788, hireDate=2018-06-21 21:24:37.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=64, dName=WorldCup, loc=Moscow]]
Emp [empNo=67, eName=瑞士队, job=MANAGER, mgr=2018, hireDate=2018-06-21 21:37:53.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=64, dName=WorldCup, loc=Moscow]]
Emp [empNo=83, eName=SET, job=主播, mgr=7788, hireDate=2018-06-28 10:08:45.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=81, dName=BigDept, loc=NEWYORK]]
Emp [empNo=16, eName=TOM, job=CODER, mgr=7788, hireDate=2018-06-28 11:18:18.0, sal=3000.0, comm=100.0, dept=Dept [deptNo=17, dName=VR, loc=Phoenix]]
Emp [empNo=18, eName=吴奇隆, job=ACTOR, mgr=7788, hireDate=2018-06-20 00:00:00.0, sal=4000.0, comm=1000.0, dept=null]
Emp [empNo=11, eName=JASON, job=CODER, mgr=7788, hireDate=2018-06-28 11:14:47.0, sal=3000.0, comm=100.0, dept=null]
Emp [empNo=5, eName=YaoMing, job=Athlete, mgr=7788, hireDate=2018-06-28 11:08:59.0, sal=3000.0, comm=100.0, dept=null]
left join 输出结果
[Hibernate] inner Join和 left Join的更多相关文章
- Hibernate中,left join、inner join以及left join fetch区别(转)
标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...
- mysql join 和left join 对于索引的问题
今天遇到一个left join优化的问题,搞了一下午,中间查了不少资料,对MySQL的查询计划还有查询优化有了更进一步的了解,做一个简单的记录: select c.* from hotel_info_ ...
- 【转】mysql的union、left join、 right join、 inner join和视图学习
1.联合 union 进行多个查询语句时,要求多次查询的结果列数必须一样.此时,查询的结果以第一个sql语句的列名为准且union会自动去重复我们应该使用union all. 例...... 1.联合 ...
- SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
在SQL语言中,存在着各种Join,有Left Join, Right Join, Inner Join, and Natural Join等,对于初学者来说肯定一头雾水,都是神马跟神马啊,它们之间到 ...
- sql语法:inner join on, left join on, right join on详细使用方法
inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有 ...
- sql之left join、right join、inner join的区别
sql之left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括 ...
- SQL中inner join、outer join和cross join的区别
对于SQL中inner join.outer join和cross join的区别简介:现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同 ...
- SQL JOIN\SQL INNER JOIN 关键字\SQL LEFT JOIN 关键字\SQL RIGHT JOIN 关键字\SQL FULL JOIN 关键字
SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. Join 和 Key 有时为了得到完整的结果,我们需要从两个或更多的表中获取结果.我们就需要执行 join. 数据库中的表 ...
- SQL表连接查询(inner join、full join、left join、right join)
SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...
随机推荐
- 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting
题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...
- SVN和Git对比梳理
在日常运维工作中,经常会用到版本控制系统,目前用到最广泛的版本控制器就是SVN和Git,那么这两者之间有什么不同之处呢?SVN(Subversion)是集中式管理的版本控制器,而Git是分布式管理的版 ...
- C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast
关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...
- 002-自定义打开terminal,以及快捷键,其他程序类似,ssh管理-sshpass, Shuttle
一.利用Automator软件完成服务设定 1.使用Command+Space,打开Spotlight,搜索Automator 2.搜索到之后,双击打开,选择“服务[或快速操作]” 3.将“服务收到[ ...
- ubuntu 安装完后对于开发需要做的事情
是从 https://www.osboxes.org/ubuntu/ 下载的vdi文件,估计vmware对应的应该也有. 1. 安装 openssh-server apt-get install op ...
- C# xml数组的序列和反序列化
先来看xml <?xml version="1.0"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSche ...
- 强迫症犯了,忍不住赞一下slf4j包Logger.java的优雅代码
如下是slf4j-api包下的Logger接口类里其中几个方法的声明: package org.slf4j; public interface Logger { /** * Log a message ...
- python_函数式编程
函数式编程是一种编程范式 (而面向过程编程和面向对象编程也都是编程范式).在面向过程编程中,我们见到过函数(function):在面向对象编程中,我们见过对象(object).函数和对象的根本目的是以 ...
- Python之多进程&异步并行
由于python的gil,多线程不是cpu密集型最好的选择 多进程可以完全独立的进程环境中运行程序,可以充分的利用多处理器 但是进程本身的隔离带来的数据不共享也是一个问题,而且线程比进程轻量 impo ...
- Nginx技术研究系列4-Nginx监控-Nginx+Telegraf+Influxb+Grafana
搭建了Nginx集群后,需要继续深入研究的就是日常Nginx监控. Nginx如何监控?相信百度就可以找到:nginx-status 通过Nginx-status,实时获取到Nginx监控数据后,如何 ...