这篇随笔将会记录hql的常用的查询语句,为日后查看提供便利。

在这里通过定义了三个类,Special、Classroom、Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,这里仅仅贴出这三个bean的属性代码:

Special类:

public class Special
{
private int id;
private String name;
private String type;
private Set<Classroom> rooms;
..........
}

Classroom类:

public class Classroom
{
private int id;
private String name;
private Special special;
private Set<Student> students;
  ............
}

Student类:

public class Student
{
private int id;
private String name;
private String sex;
private Classroom room;
..........
}

1.最简单的查询

List<Special> specials = (List<Special>)session.createQuery("select spe from Special spe").list();

这是hql最基本的查询语句了,作用就是查出所有的Special对象放到一个List当中

2.基于 ? 的参数化形式

       /**
* 查询中使用?,通过setParameter的方式可以防止sql注入
* jdbc的setParameter的下标从1开始,hql的下标从0开始
*/
List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like ?")
.setParameter(0, "%刘%")
.list();

在hql中同样支持基于 ? 的参数化形式查询,注意:在jdbc中,setParameter的下标是从1开始的,而hibernate的setParameter的下标是从0开始的。

3.基于 :xx 的别名的方式设置参数

       /**
* 在hql中可以使用别名的方式来查询,格式是 :xxx 通过setParameter来设置别名
*/
List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like :name and sex like :sex")
.setParameter("name", "%王%").setParameter("sex", "%男%")
.list();

4.如果返回的值只有一个,可以使用uniqueResult方法

       /**
* 如果得到的值只有一个,则可以使用uniqueResult方法
*/
Long stu = (Long)session.createQuery("select count(*) from Student stu where name like :name and sex like :sex")
.setParameter("name", "%王%").setParameter("sex", "%男%")
.uniqueResult();        /**
* 如果得到的值只有一个,则可以使用uniqueResult方法
*/
Student stu = (Student)session.createQuery("select stu from Student stu where id = ?")
.setParameter(0, 1)
.uniqueResult();

5.基于投影的查询

       /**
* 基于投影的查询,如果返回多个值,这些值都是保存在一个object[]数组当中
*/
List<Object[]> stus = (List<Object[]>)session.createQuery("select stu.name, stu.sex from Student stu where name like
                            :name and sex like :sex")
.setParameter("name", "%张%").setParameter("sex", "%男%")
.list();

6.基于导航对象的查询

       /**
* 如果对象中有导航对象,可以直接通过对象导航查询
*/
List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex")
.setParameter("room", "%计算机应用%").setParameter("sex", "%女%")
.list();

注意:若直接通过导航对象来查询时,其实际是使用cross join(笛卡儿积)来进行连接查询,这样做性能很差,不建议使用

7.使用 in 进行列表查询

       /**
* 可以使用in设置基于列表的查询,使用in查询时需要使用别名来进行参数设置,
* 通过setParameterList方法即可设置,在使用别名和?的hql语句查询时,?形式的查询必须放在别名前面
*/
// List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where sex like ? and stu.room.id in (:room)")
// .setParameter(0, "%女%").setParameterList("room", new Integer[]{1, 2})
// .list();
List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.id in (:room) and stu.sex like :sex")
.setParameterList("room", new Integer[]{1, 2}).setParameter("sex", "%女%")
.list();

在使用 in 进行列表查询时,这个时候要通过 setParameterList() 方法来设置我们的参数,注意:如果一个参数通过别名来传入,一个是通过 ? 的方式来传入的话,那么通过别名的hql语句以及参数设置语句要放在 ? 的后面,不然hibernate会报错。如果都是使用 别名 来设置参数,则无先后顺序

8.分页查询

       /**
* 通过setFirstResult(0).setMaxResults(10)可以设置分页查询,相当于offset和pagesize
*/
List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex")
.setParameter("room", "%计算机应用%").setParameter("sex", "%女%").setFirstResult(0).setMaxResults(10)
.list();

9.内连接查询

       /**
* 使用对象的导航查询可以完成连接查询,但是使用的是Cross Join(笛卡儿积),效率不高,所以建议使用join来查询
*/
List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu join stu.room room where room.id=2")
.list();

在hql中使用连接查询的语句与我们的sql进行连接查询的语句是有区别的:

hql:    select stu from Student stu join stu.room room

sql:    select t.* from Student t join Classroom c on t.cid=c.id

10.左外连和右外连查询

       /**
* 左外连和右外连其实是相对的,left join 就是以左边的表为基准, right join 就是以右边的表为基准
*/
List<Object[]> stus = (List<Object[]>)session.createQuery("select room.name, count(stu.room.id) from Student stu right join stu.room room group by room.id")
.list();

11.创建DTO类,将查询出来的多个字段可以存放到DTO对象中去

       /**
* 当如果我们查询出多个字段的话,通常会创建一个DTO对象,用来存储我们查询出来的数据,通过 new XXX() 这样的方式
* 前提是XXX这个类里面必须要有接受这些字段的构造方法才行,而且必须要使用类的全名
*/
// List<Object[]> stus = (List<Object[]>)session.createQuery("select stu.id,stu.name,stu.sex,room.name,special.name from Student stu left join stu.room room left join room.special special")
// .list();
// for(Object[] obj : stus)
// {
// System.out.println(obj[0] + ", " + obj[1] + ", " + obj[2] + ", " + obj[3] + ", " + obj[4]);
// } List<StudentDTO> stus = (List<StudentDTO>)session.createQuery("select new com.xiaoluo.bean.StudentDTO(stu.id, stu.name, stu.sex, room.name, special.name) from Student stu left join stu.room room left join room.special special")
                             .list();

12.group having字句

/**
* 在hql中不能通过给查询出来的字段设置别名,别名只能设置在from 后面
*/
List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id having count(stu.room.special.id)>150")
.list();  // 查询出人数大于150个人的专业
       
       //  查询出每个专业中男生与女生的个数
       List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, stu.sex, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id,stu.sex")
.list();
 
 

Hibernate中Hql查询的更多相关文章

  1. Hibernate 中Hql 查询中间表的用法

    案例简述: 项目中存在User 用户表 和 Role 角色表 它们之间是多对多的关系 在User类定义中 使用hibernate注解 //角色列表 @ManyToMany(targetEntity = ...

  2. hibernate 中HQL查询

    由于比较简单,在此处只写一些HQL语言. 表关系,多对一. CREATE TABLE `user` ( `id` ) NOT NULL AUTO_INCREMENT, `uname` varchar( ...

  3. hibernate的hql查询

    1.概念介绍 1.Query是Hibernate的查询接口,用于从数据存储源查询对象及控制执行查询的过程,Query包装了一个HQL查询语句. 2.HQL是Hibernate Query Langua ...

  4. Hibernate之HQL查询

    一.Hibernate 提供了以下几种检索对象的方式: 导航对象图检索方式: 根据已经加载的对象导航到其他对象 OID 检索方式: 按照对象的 OID 来检索对象 HQL 检索方式:使用面向对象的 H ...

  5. Hibernate五 HQL查询

    HQL查询一 介绍1.HQL:Hibernate Query Language,是一种完全面向对象的查询语言.使用Hibernate有多重查询方式可供选择:hibernate的HQL查询,也可以使用条 ...

  6. Hibernate 的hql查询简介【申明:来源于网络】

    Hibernate 的hql查询简介[申明:来源于网络] Hibernate 的hql查询简介:http://blog.csdn.net/leaf_130/article/details/539329 ...

  7. JavaWeb_(Hibernate框架)Hibernate中数据查询语句HQL基本用法

    HQL(Hibernate Query Language) 是面向对象的查询语言, 它和 SQL 查询语言有些相似. 在 Hibernate 提供的各种检索方式中, HQL 是使用最广的一种检索方式. ...

  8. hibernate中HQL练习时候一个小小的错误导致语法异常

    package cn.db.po.test; import java.util.List; import cn.db.po.User; import cn.db.po.biz.UserBiz; pub ...

  9. hibernate中带查询条件的分页

    所谓分页,从数据库中分,则是封装一个分页类.利用分页对象进行分页. 但,分页往往带查询条件. 分页类的三个重要数据:[当前页码数],[数据库中的总记录数],[每页显示的数据的条数] 原理:select ...

随机推荐

  1. RT-Thread内核之线程调度(三)

    4.RT-Thread中的线程? /**  * 线程结构  */ struct rt_thread {     /** Object对象 */     char        name[RT_NAME ...

  2. 查看Oracle的表中有哪些索引

    用user_indexes和user_ind_columns系统表查看已经存在的索引 对于系统中已经存在的索引我们可以通过以下的两个系统视图(user_indexes和user_ind_columns ...

  3. 简明python教程三-----函数

    函数通过def关键字定义.def关键字后跟一个函数的表标识符名称,然后跟一对圆括号. 圆括号之中可以包括一些变量名,该行以冒号结尾.接下来是一块语句,它们是函数体. def sayHello(): p ...

  4. Spark SQL原理和实现--王家林老师

  5. Java io流详解三

    public class IOpractise { public void iotest() { int b= 0; FileInputStream fis = null; try { fis = n ...

  6. Ubuntu 16.04 安装Django

    > pip install django==1.10.3......或者:> pip3 install django==1.10.3(我采用)......或者:>python3 -m ...

  7. 移植MarS Board代码到内核3.0.35

    MarS Board提供的出厂Linux内核是3.0.15的.而Freescale的BSP都早已经更新到3.0.35.为了跟上节奏,我花了点时间把关于marsboard代码从3.0.15移植到了Fre ...

  8. jQuery音乐播放器jPlayer

    在线演示 本地下载

  9. jenkins添加GIT repository报错

    添加了ssh互信,但一直提示如下错误. Failed to connect to repository : Command "git ls-remote -h git@git.xxx.cn: ...

  10. vuex初使用