背景
基于上一章节《Hibernate(十二):HQL查询(一)》,已经学习了一部分关于HQL的用法:
- HQL带参数查询
- HQL Order By排序查询
- HQL 设置实体参数查询
本章节将会学习:HQL分页查询、HQL命名语句查询、HQL投影查询、HQL报表查询。
HQL分页查询
1)setFirstResult(int firstResult):设定从哪一个对象开始检索,参数firstResult表示这个对象在查询结果中的索引位置,索引位置的起始值为0。默认情况下,Query从查询结果中的第一个对象开始检索。
2)setMaxResults(int maxResults):设定一次最多检索出的对象的数目。默认情况下,Query和Criteria接口检索出查询结果中所有的对象。
@Test
public void testHQLPageQuery() {
String hql = "From Employee"; int pageNum = 1;
int pageSize = 10;
Query query = session.createQuery(hql).setFirstResult((pageNum - 1) * pageSize).setMaxResults(pageSize); List<Employee> employees = query.list();
System.out.println(employees.size());
System.out.println(employees);
}
执行sql及结果:
Hibernate:
select
employee0_.ID as ID1_1_,
employee0_.NAME as NAME2_1_,
employee0_.SALARY as SALARY3_1_,
employee0_.EMAIL as EMAIL4_1_,
employee0_.DEPARTMENT_ID as DEPARTME5_1_
from
DX_EMPLOYEE employee0_ limit ?
10
[Employee [id=1, name=tommy0, salary=0.0, email=tommy0@dx.com]
, Employee [id=2, name=tommy1, salary=1000.0, email=tommy1@dx.com]
, Employee [id=3, name=tommy2, salary=2000.0, email=tommy2@dx.com]
, Employee [id=4, name=tommy3, salary=3000.0, email=tommy3@dx.com]
, Employee [id=5, name=tommy4, salary=4000.0, email=tommy4@dx.com]
, Employee [id=6, name=tommy5, salary=5000.0, email=tommy5@dx.com]
, Employee [id=7, name=tommy6, salary=6000.0, email=tommy6@dx.com]
, Employee [id=8, name=tommy7, salary=7000.0, email=tommy7@dx.com]
, Employee [id=9, name=tommy8, salary=8000.0, email=tommy8@dx.com]
, Employee [id=10, name=tommy9, salary=9000.0, email=tommy9@dx.com]
]
HQL命名语句查询
在映射文件中定义命名查询语句:
1)Hibernate允许在映射文件中定义字符串形式的查询语句;
2)<query>元素用于定义一个HQL查询语句,它和<class>元素并列;
<query name="findNewsByContent">
<![CDATA[From News n Where n.content like :keyword]]>
</query>
3)在程序中,通过Session的getNamedQuery()方法获取查询语句对应的Query对象。
修改Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-6-9 23:13:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate09.hql01.Employee" table="DX_EMPLOYEE">
。。。
</class>
<query name="queryWithSalaryScope">
<![CDATA[From Employee e Where e.salary>:minSalary and e.salary<:maxSalary]]>
</query>
</hibernate-mapping>
测试函数
@Test
public void testHQLPageQuery() {
String hql = "From Employee"; int pageNum = 1;
int pageSize = 10;
Query query = session.createQuery(hql).setFirstResult((pageNum - 1) * pageSize).setMaxResults(pageSize); List<Employee> employees = query.list();
System.out.println(employees.size());
System.out.println(employees);
}
执行sql及结果
Hibernate:
select
employee0_.ID as ID1_1_,
employee0_.NAME as NAME2_1_,
employee0_.SALARY as SALARY3_1_,
employee0_.EMAIL as EMAIL4_1_,
employee0_.DEPARTMENT_ID as DEPARTME5_1_
from
DX_EMPLOYEE employee0_
where
employee0_.SALARY>?
and employee0_.SALARY<?
4
[Employee [id=7, name=tommy6, salary=6000.0, email=tommy6@dx.com]
, Employee [id=8, name=tommy7, salary=7000.0, email=tommy7@dx.com]
, Employee [id=9, name=tommy8, salary=8000.0, email=tommy8@dx.com]
, Employee [id=10, name=tommy9, salary=9000.0, email=tommy9@dx.com]
]
HQL投影查询
1)投影查询:查询结果仅包含实体的部分属性。通过SELECT关键字实现。
2)Query的list()方法返回的结果集中包含的是数组类型的元素,每个对象数组代表查询结果的一条记录。
3)可以在持久化类中定义一个对象的构造器来包装投影查询返回的记录,是程序代码能完成运用面向对象的语义来访问查询结果集。
4)可以通过DISTINCT关键字来保证查询结果不会返回重复元素。
返回数据测试:
@Test
public void testHQLFileds() {
String hql = "Select e.email,e.salary,e.department From Employee e Where e.salary>:salary and e.department=:department";
Department department = new Department();
department.setId(3);
Query query = session.createQuery(hql).setFloat("salary", 10000).setEntity("department", department); List<Object[]> result = query.list(); for (Object[] item : result) {
System.err.println(Arrays.asList(item));
}
}
执行sql语句及结果:
Hibernate:
select
employee0_.EMAIL as col_0_0_,
employee0_.SALARY as col_1_0_,
employee0_.DEPARTMENT_ID as col_2_0_,
department1_.ID as ID1_0_,
department1_.NAME as NAME2_0_
from
DX_EMPLOYEE employee0_
inner join
DX_DEPARTMENT department1_
on employee0_.DEPARTMENT_ID=department1_.ID
where
employee0_.SALARY>?
and employee0_.DEPARTMENT_ID=?
[tommy12@dx.com, 12000.0, Department [id=3, name=业务部门]]
[tommy17@dx.com, 17000.0, Department [id=3, name=业务部门]]
[tommy22@dx.com, 22000.0, Department [id=3, name=业务部门]]
[tommy27@dx.com, 27000.0, Department [id=3, name=业务部门]]
[tommy32@dx.com, 32000.0, Department [id=3, name=业务部门]]
[tommy37@dx.com, 37000.0, Department [id=3, name=业务部门]]
[tommy42@dx.com, 42000.0, Department [id=3, name=业务部门]]
[tommy47@dx.com, 47000.0, Department [id=3, name=业务部门]]
[tommy52@dx.com, 52000.0, Department [id=3, name=业务部门]]
[tommy57@dx.com, 57000.0, Department [id=3, name=业务部门]]
[tommy62@dx.com, 62000.0, Department [id=3, name=业务部门]]
[tommy67@dx.com, 67000.0, Department [id=3, name=业务部门]]
[tommy72@dx.com, 72000.0, Department [id=3, name=业务部门]]
[tommy77@dx.com, 77000.0, Department [id=3, name=业务部门]]
上边我们看到如果返回数据,用起来多少会有点不爽,是可以在持久化类中定义一个对象的构造器来包装投影查询返回的记录,是程序代码能完成运用面向对象的语义来访问查询结果集。
修改上边的代码:
@Test
public void testHQLFileds_() {
String hql = "Select new Employee(e.email,e.salary,e.department) From Employee e Where e.salary>:salary and e.department=:department";
Department department = new Department();
department.setId(3);
Query query = session.createQuery(hql).setFloat("salary", 10000).setEntity("department", department); List<Employee> result = query.list(); for (Employee item : result) {
System.err.println(item.getId() + "," + item.getEmail() + "," + item.getSalary() + "," + item.getDepartment());
}
}
执行sql及结果:
Hibernate:
select
employee0_.EMAIL as col_0_0_,
employee0_.SALARY as col_1_0_,
employee0_.DEPARTMENT_ID as col_2_0_
from
DX_EMPLOYEE employee0_
inner join
DX_DEPARTMENT department1_
on employee0_.DEPARTMENT_ID=department1_.ID
where
employee0_.SALARY>?
and employee0_.DEPARTMENT_ID=?
Hibernate:
select
department0_.ID as ID1_0_0_,
department0_.NAME as NAME2_0_0_
from
DX_DEPARTMENT department0_
where
department0_.ID=?
null,tommy12@dx.com,12000.0,Department [id=3, name=业务部门]
null,tommy17@dx.com,17000.0,Department [id=3, name=业务部门]
null,tommy22@dx.com,22000.0,Department [id=3, name=业务部门]
null,tommy27@dx.com,27000.0,Department [id=3, name=业务部门]
null,tommy32@dx.com,32000.0,Department [id=3, name=业务部门]
null,tommy37@dx.com,37000.0,Department [id=3, name=业务部门]
null,tommy42@dx.com,42000.0,Department [id=3, name=业务部门]
null,tommy47@dx.com,47000.0,Department [id=3, name=业务部门]
null,tommy52@dx.com,52000.0,Department [id=3, name=业务部门]
null,tommy57@dx.com,57000.0,Department [id=3, name=业务部门]
null,tommy62@dx.com,62000.0,Department [id=3, name=业务部门]
null,tommy67@dx.com,67000.0,Department [id=3, name=业务部门]
null,tommy72@dx.com,72000.0,Department [id=3, name=业务部门]
null,tommy77@dx.com,77000.0,Department [id=3, name=业务部门]
HQL报表查询
报表查询用于对数据分组和统计,与SQL一样,HQL利用GROUP BY关键字对数据分组,用HAVING关键字对分组数据设定约束条件。
在HQL查询语句中可以调用以下聚合函数:
- count()
- min()
- max()
- sum()
- avg()
测试代码
@Test
public void testGroupBy() {
String hql = "Select min(e.salary),max(e.salary) "
+ "From Employee e "
+ "group by e.department having(min(e.salary)>:minSalary)";
Query query = session.createQuery(hql).setFloat("minSalary", 0); List<Object[]> result = query.list();
for (Object[] item : result) {
System.out.println(Arrays.asList(item));
}
}
执行sql与结果
Hibernate:
select
min(employee0_.SALARY) as col_0_0_,
max(employee0_.SALARY) as col_1_0_
from
DX_EMPLOYEE employee0_
group by
employee0_.DEPARTMENT_ID
having
min(employee0_.SALARY)>?
[1000.0, 76000.0]
[2000.0, 77000.0]
[3000.0, 78000.0]
[4000.0, 79000.0]