HQL常用连接类型
- 内连接 inner join 或 join
- 迫切内连接 inner join fetch 或 join fetch
- 左外连接 left outer join 或 left join
- 迫切左外连接 left outer join fetch 或 left join fetch
- 右外连接 right outer join 或 right join
内连接 获取的就是两个表中的交集部分
左连接会读取左边数据表的全部数据,即使右边数据表没有对应数据。
右连接会读取右边数据表的全部数据,即使左边数据表没有对应数据。
内连接 查询部门和员工信息 from Entity [inner] join Entity.property
List
Hibernate:
select
department0_.deptNo as deptNo0_0_,
emps1_.empNo as empNo1_1_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_,
emps1_.empName as empName1_1_,
emps1_.job as job1_1_,
emps1_.salary as salary1_1_,
emps1_.deptNo as deptNo1_1_
from
project.Department department0_
inner join
project.Emp emps1_
on department0_.deptNo=emps1_.deptNo
list集合中的每个元素都是一个Object数组,数组的第一个元素时Dept元素对象,第二个元素时Emp对象,Dept对象的emps集合元素没有被初始化,即emps集合里没有存放关联的Emp对象。Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception.
迫切内连接查询部门和员工信息from Entity [inner] join fetch Entity.property
List objects=this.getCurrentSession().createQuery("select distinct d from Department d join fetch d.emps").list();
for(Department department:objects)
{
System.out.println(department.getEmps());
}
Hibernate:
select
department0_.deptNo as deptNo0_0_,
emps1_.empNo as empNo1_1_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_,
emps1_.empName as empName1_1_,
emps1_.job as job1_1_,
emps1_.salary as salary1_1_,
emps1_.deptNo as deptNo1_1_,
emps1_.deptNo as deptNo0_0__,
emps1_.empNo as empNo0__
from
project.Department department0_
inner join
project.Emp emps1_
on department0_.deptNo=emps1_.deptNo
list集合中的每个元素都是Dept对象,Hibernate使用fetch关键字实现了将Emp对象读出来后立即填充到对应的Dept对象的集合属性中。如果有一些Dept对象,可以使用select distinct d from Dept d inner join fetch d.emps解决。
左外连接 from Entity left [outer] join Entity.property
List
Hibernate:
select
department0_.deptNo as deptNo0_0_,
emps1_.empNo as empNo1_1_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_,
emps1_.empName as empName1_1_,
emps1_.job as job1_1_,
emps1_.salary as salary1_1_,
emps1_.deptNo as deptNo1_1_
from
project.Department department0_
left outer join
project.Emp emps1_
on department0_.deptNo=emps1_.deptNo
迫切左外连接from Entity left [outer] join fetch Entity.property
List objects=this.getCurrentSession().createQuery("select distinct d from Department d left join fetch d.emps").list();
for (Department department:objects)
{
System.out.println(department.getEmps());
}
Hibernate:
select
distinct department0_.deptNo as deptNo0_0_,
emps1_.empNo as empNo1_1_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_,
emps1_.empName as empName1_1_,
emps1_.job as job1_1_,
emps1_.salary as salary1_1_,
emps1_.deptNo as deptNo1_1_,
emps1_.deptNo as deptNo0_0__,
emps1_.empNo as empNo0__
from
project.Department department0_
left outer join
project.Emp emps1_
on department0_.deptNo=emps1_.deptNo
右外连接 from Entity right [outer] join Entity.property
List
Hibernate:
select
department0_.deptNo as deptNo0_0_,
emps1_.empNo as empNo1_1_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_,
emps1_.empName as empName1_1_,
emps1_.job as job1_1_,
emps1_.salary as salary1_1_,
emps1_.deptNo as deptNo1_1_
from
project.Department department0_
right outer join
project.Emp emps1_
on department0_.deptNo=emps1_.deptNo
隐式内连接 按部门条件查询员工信息
List emps=this.getCurrentSession().createQuery("from Emp e where e.department.deptNo=:deptNo")
.setParameter("deptNo",deptNo)
.list();
Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_,
emp0_.deptNo as deptNo1_
from
project.Emp emp0_
where
emp0_.deptNo=?
Hibernate:
select
department0_.deptNo as deptNo0_0_,
department0_.deptName as deptName0_0_,
department0_.location as location0_0_
from
project.Department department0_
where
department0_.deptNo=?
List
Hibernate:
select
emp0_.empName as col_0_0_,
department1_.deptName as col_1_0_
from
project.Emp emp0_,
project.Department department1_
where
emp0_.deptNo=department1_.deptNo