1. 对于HQL而言,都是基于对象进行查询的
Query query = session.createQuery("from Special");
Listspes = query.list();
注意:HQL不能使用select *进行查询
2.可以使用链式查询的方式:
List<Special>spes = session.createQuery("select spe from Special spe").list();
3.模糊查询:
List<Special> stus = session.createQuery("from Student where name like '%张%'").list();List
4.基于?的条件的查询,特别注意:jdbc设置参数的最小下标是1,hibernate是0
List<Special>stus = session.createQuery("from Student where name like ?").setParameter(0, "%李%").list();
5.还可以基于别名进行查询,使用:xxx来说明别名的名称
List<Special> stus = session.createQuery("from Student where name like :name and sex=:sex").setParameter("name", "%刘%").setParameter("sex", "男").list();
6.使用uniqueResult可以返回唯一的一个值
Long stus = (Long)session.createQuery("select count(*) from Student where name like :name and sex=:sex").setParameter("name", "%刘%").setParameter("sex", "男").uniqueResult();
Student stu = (Student)session.createQuery("select stu from Student stu where id=:id")
.setParameter("id", 1).uniqueResult();
7.基于投影的查询,通过在列表中存储一个对象的数组
List <Object[]>stus = session.createQuery("select stu.sex,count(*) from Student stu group by stu.sex").list();
8.如果对象中相应的导航对象,可以直接导航完成查询
List<Student> stus = session.createQuery("select stu from Student stu where stu.classroom.name=? and stu.name like ?").setParameter(0, "计算机教育班").setParameter(1, "%张%").list();
9.可以使用in来设置基于列表的查询,此处的查询需要使用别名进行查询
* 特别注意,使用in的查询必须在其他的查询之后
Liststus = session.createQuery("select stu from Student stu where stu.name like ? and stu.classroom.id in (:clas)").setParameter(0, "%张%").setParameterList("clas", new Integer[]{1,2}).list();
10.使用setFirstResult和setMaxResult可以完成分页的offset和pageSize的设置
List<Student> stus = session.createQuery("select stu from Student stu where stu.classroom.id in (:clas)").setParameterList("clas", new Integer[]{1,2}).setFirstResult(0).setMaxResults(15).list();
11.可以通过is null来查询为空的对象,和sql一样不能使用=来查询null的对象
List<Student>stus = session.createQuery("select stu from Student stu where stu.classroom is null").setFirstResult(0).setMaxResults(15).list();
12.使用对象的导航可以完成连接,但是是基于Cross JOIN,效率不高,可以直接使用JOIN来完成连接
List<Student>stus = session.createQuery("select cla.name,count(stu.classroom.id) from Student stu right join stu.classroom cla group by cla.id").list();
13.直接可以使用new XXObject完成查询,注意,一定要加上Object的完整包名
* 这里使用的new XX,必须在对象中加入相应的构造函数
注意:需要新建一个实体类,没有存储意义,但是具有传输意义
List<StudentDto>stus = session.createQuery("select new org.zttc.itat.model.StudentDto" +"(stu.id as sid,stu.name as sname,stu.sex as sex,cla.name as cname,spe.name as spename) " +"from Student stu left join stu.classroom cla left join cla.special spe").list();
14.having是为group来设置条件的
List<Student>stus = session.createQuery("select spe.name," +"(count(stu.classroom.special.id)) from Student stu right join " +"stu.classroom.special spe group by spe having count(stu.classroom.special.id)>150").list();
having是为group来设置条件的
List<Student>stus = session.createQuery("select stu.sex,spe.name," +"(count(stu.classroom.special.id)) from Student stu right join " +"stu.classroom.special spe group by spe,stu.sex").list();