内连接:
查询出来的每一项为 object[]数组,左表存在object[0]中,右表存在object[1]中,object[0]和object[1]的关联关系有没有设置,取决于你的配置文件中,关联的配置时候lazy的值,如果lazy=false,则马上维护关系,lazy=true,先不维护关系,等用到关联关系维系的值时才查询。
- Session session = DBUtil.getSession();
- String hql = "from Deptment d inner join d.employees";
- Query query = session.createQuery(hql);
- //如果设置lazy=”false",这时候就会关联部门和员工,除了发送内连接语句外,还发送了查询每个
- //部门下有哪些员工的sql语句
- List list = query.list();
- Iterator iter = list.iterator();
- while(iter.hasNext())
- {
- Object[] object = (Object[]) iter.next();
- System.out.println("部门信息:");
- System.out.println(object[0]);
- Deptment dept = (Deptment)object[0];
- //如果设置lazy="true"的时候,这时候才去查询部门的员工,发sql语句
- System.out.println(dept.getEmployees());
- System.out.println("员工信息:");
- System.out.println(object[1]);
- }
- session.close();
结果1(设置关联关系的维护为lazy=true):
- Hibernate:
- select
- deptment0_.deptid as deptid0_0_,
- employees1_.epno as epno1_1_,
- deptment0_.deptname as deptname0_0_,
- employees1_.deptid as deptid1_1_,
- employees1_.epname as epname1_1_
- from
- deptment deptment0_
- inner join
- employee employees1_
- on deptment0_.deptid=employees1_.deptid
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- Hibernate:
- select
- employees0_.deptid as deptid1_,
- employees0_.epno as epno1_,
- employees0_.epno as epno1_0_,
- employees0_.deptid as deptid1_0_,
- employees0_.epname as epname1_0_
- from
- employee employees0_
- where
- employees0_.deptid=?
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 员工信息:
- Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 员工信息:
- Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- Hibernate:
- select
- employees0_.deptid as deptid1_,
- employees0_.epno as epno1_,
- employees0_.epno as epno1_0_,
- employees0_.deptid as deptid1_0_,
- employees0_.epname as epname1_0_
- from
- employee employees0_
- where
- employees0_.deptid=?
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 员工信息:
- Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 员工信息:
- Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]
结果2(设置关联关系的维护lazy=false,关联关系立马维护,产生多条sql语句)
- Hibernate:
- select
- deptment0_.deptid as deptid0_0_,
- employees1_.epno as epno1_1_,
- deptment0_.deptname as deptname0_0_,
- employees1_.deptid as deptid1_1_,
- employees1_.epname as epname1_1_
- from
- deptment deptment0_
- inner join
- employee employees1_
- on deptment0_.deptid=employees1_.deptid
- Hibernate:
- select
- employees0_.deptid as deptid1_,
- employees0_.epno as epno1_,
- employees0_.epno as epno1_0_,
- employees0_.deptid as deptid1_0_,
- employees0_.epname as epname1_0_
- from
- employee employees0_
- where
- employees0_.deptid=?
- Hibernate:
- select
- employees0_.deptid as deptid1_,
- employees0_.epno as epno1_,
- employees0_.epno as epno1_0_,
- employees0_.deptid as deptid1_0_,
- employees0_.epname as epname1_0_
- from
- employee employees0_
- where
- employees0_.deptid=?
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 员工信息:
- Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 员工信息:
- Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 员工信息:
- Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 员工信息:
- Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]
迫切内连接(不管设置lazy=true或者lazy=false,他关联关系立马就维护好了,且只发送一条sql语句,且结果集中的每条结果为 对象(本例中为部门对象,部门里面的员工已经填充好了)
- Session session = DBUtil.getSession();
- String hql = "from Deptment d inner join fetch d.employees";
- Query query = session.createQuery(hql);
- List list = query.list();
- Iterator iter = list.iterator();
- while(iter.hasNext())
- {
- Deptment object = (Deptment) iter.next();
- System.out.println("部门信息:");
- System.out.println(object);
- System.out.println("员工信息:");
- System.out.println(object.getEmployees());
- }
结果:
- Hibernate:
- select
- deptment0_.deptid as deptid0_0_,
- employees1_.epno as epno1_1_,
- deptment0_.deptname as deptname0_0_,
- employees1_.deptid as deptid1_1_,
- employees1_.epname as epname1_1_,
- employees1_.deptid as deptid0__,
- employees1_.epno as epno0__
- from
- deptment deptment0_
- inner join
- employee employees1_
- on deptment0_.deptid=employees1_.deptid
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
左外连接:在内连接的基础上,保证左表的数据不能丢失,如果左表中没有对应的右表数据,则右表对象置为空。lazy的设置导致的结果影响和内连接相同。
代码:
- Session session = DBUtil.getSession();
- String hql = "from Deptment d left join d.employees";
- Query query = session.createQuery(hql);
- List list = query.list();
- Iterator iter = list.iterator();
- while(iter.hasNext())
- {
- Object[] object = (Object[]) iter.next();
- System.out.println("部门信息:");
- System.out.println(object[0]);
- System.out.println("员工信息:");
- if(object[1]!=null){
- System.out.println(object[1]);
- }else{
- System.out.println("null");
- }
- }
- session.close();
结果(lazy=true 的时候),我们可以看到最后多了一个null:
- Hibernate:
- select
- deptment0_.deptid as deptid0_0_,
- employees1_.epno as epno1_1_,
- deptment0_.deptname as deptname0_0_,
- employees1_.deptid as deptid1_1_,
- employees1_.epname as epname1_1_
- from
- deptment deptment0_
- left outer join
- employee employees1_
- on deptment0_.deptid=employees1_.deptid
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]
- 部门信息:
- Deptment [deptid=2, deptname=aaa]
- 员工信息:
- null
迫切左外连接:
- Session session = DBUtil.getSession();
- String hql = "from Deptment d left join fetch d.employees";
- Query query = session.createQuery(hql);
- List list = query.list();
- Iterator iter = list.iterator();
- while(iter.hasNext())
- {
- Deptment object = (Deptment) iter.next();
- System.out.println("部门信息:");
- System.out.println(object);
- System.out.println("员工信息:");
- System.out.println(object.getEmployees());
- }
结果:
- Hibernate:
- select
- deptment0_.deptid as deptid0_0_,
- employees1_.epno as epno1_1_,
- deptment0_.deptname as deptname0_0_,
- employees1_.deptid as deptid1_1_,
- employees1_.epname as epname1_1_,
- employees1_.deptid as deptid0__,
- employees1_.epno as epno0__
- from
- deptment deptment0_
- left outer join
- employee employees1_
- on deptment0_.deptid=employees1_.deptid
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 部门信息:
- Deptment [deptid=1, deptname=aaa]
- 员工信息:
- [Employee [epno=2, deptment=Deptment [deptid=1, deptname=aaa], epname=shizhan2], Employee [epno=1, deptment=Deptment [deptid=1, deptname=aaa], epname=null]]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 部门信息:
- Deptment [deptid=3, deptname=sdsdzz]
- 员工信息:
- [Employee [epno=3, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=xsxs], Employee [epno=4, deptment=Deptment [deptid=3, deptname=sdsdzz], epname=ttt]]
- 部门信息:
- Deptment [deptid=2, deptname=aaa]
- 员工信息:
- []