zoukankan      html  css  js  c++  java
  • [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
    left join输出24条结果,因为3条是没有deptno的
    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 输出结果
    宛如智障,暗藏锋芒
  • 相关阅读:
    SDN课程阅读作业(2)
    2019 SDN上机第5次作业
    第05组 Alpha事后诸葛亮
    第05组 Alpha冲刺(4/4)
    2020-4-5助教一周小结
    2020-3-29助教一周小结
    2020-3-22助教一周小结
    2020-03-15助教一周小结
    2020-03-08助教一周小结
    第一次个人编程作业(个人实现)
  • 原文地址:https://www.cnblogs.com/zienzir/p/9238393.html
Copyright © 2011-2022 走看看