概述

HQL是Hibernate封装成为面向对象的数据库查询语言,具有如下特点:

  • 面向对象,包括继承、多态和关联之类的概念,SQL操作的数据库的表,HQL更像是操作对象
  • 大小写敏感,只对对象和属性敏感,关键字不区分大小写,区别于SQL

简单查询语句

在看完HQL的特点之后,相信大家对于HQL的掌握有一定想法,我觉得就是使用面向对象的思维结合SQL的语法来学习HQL是比较快速且便于理解的。

现举例说明,假设有Course类(表),Subject学科类别,CourseTable课程表,相互关系如下:

Course对Subject:多对多双向,即Course中有一个Subject的Set,Subject中有一个Course的Set

Course对CourseTable:多对一单向,即CourseTable中有一个Course对象

查询所有课程

from Course

查询课程名称为math的课程



  1. from Course as course where course.name='math'
from Course as course where course.name='math'

查询课程名称为math的课程总数



  1. select count(*) from Course as course where course.name='math'
select count(*) from Course as course where course.name='math'

查询所有课程名称为math的课程,按id升序



  1. select count(*) from Course as course where course.name='math' order by course.id asc
select count(*) from Course as course where course.name='math' order by course.id asc

查询所有课程,按id升序,name降序



  1. select count(*) from Course as course order by course.id asc, course.name desc
select count(*) from Course as course order by course.id asc, course.name desc

注意:

  • Course指的是类而不是表,所以要区分大小写
  • course.id指的是course对象的属性,而不是表的列,注意大小写
  • HQL也支持使用函数,包括avg,sum,count,min,max

连接表查询

HQL的表连接参考了ANSI SQL,包含以下集中连接方式(括弧表示outer可以不写):

  • inner join内连接
  • left (outer) join 左外链接
  • right(outer) join 右外链接
  • full join 全连接(不常用)

内连接inner join

内连接结果中包含两张表中符合条件的行。

查询属于计算机科学学科(Subject)的所有课程(Course中有一个Subject Set)

显式内连接:



  1. from Course as course inner join course.subjects as subject where subject.name='计算机科学'
from Course as course inner join course.subjects as subject where subject.name='计算机科学'

注意上述查询语句的结果是List<Object[]>,每个Object数据包含Course和Subject对象,如果要只返回Course:



  1. select distinct course

  2. from Course as course

  3. inner join course.subjects as subject

  4. where subject.name='计算机科学'
select distinct course
from Course as course
inner join course.subjects as subject
where subject.name='计算机科学'

隐式内连接,如果课程和Subject是一对一的关系:



  1. from Course as course where course.subject.name='计算机科学'
from Course as course where course.subject.name='计算机科学'

查询的结果直接就是Course对象的list

外连接

左外链接,包含两张表所有列,保留左面表的所有行,右面表没有的行null填充



  1. select distinct course

  2. from Course as course

  3. left join course.subjects as subject

  4. where subject.name='计算机科学'
select distinct course
from Course as course
left join course.subjects as subject
where subject.name='计算机科学'

查询结果也是List<Object[]>,如果要只返回Course对象,使用select distinct course

右外链接,包含两张表所有列,保留右面表的所有行,左面表没有的行null填充



  1. select distinct course

  2. from Course as course

  3. right join course.subjects as subject

  4. where subject.name='计算机科学'
select distinct course
from Course as course
right join course.subjects as subject
where subject.name='计算机科学'

多表查询

查询某张课程表(CourseTable)里面的所有课程(Course),课程表对课程多对一,单向



  1. from Course as course, CourseTable as courseTable

  2. where course.id=courseTable.course.id
from Course as course, CourseTable as courseTable
where course.id=courseTable.course.id

查询结果为List<Object[]>,每个Object[]包含Course和CourseTable,如果只查询course,使用select distinct course

web进修之—Hibernate HQL(7)的更多相关文章

  1. web进修之—Hibernate起步(1)(2)

    想开始写博客了,尝试了CSDN和cnblog之后还是觉得cnblog更加简洁.专注(不过cnblog不支持搬家),所以把刚刚写的两篇学习博客链接放在这儿,这样这个系列也算是完整了: web进修之—Hi ...

  2. web进修之—Hibernate 关系映射(3)

    概述 Hibernate的关系映射是Hibernate使用的难点或者是重点(别担心,不考试哦~),按照不同的分类方式可以对这些映射关系做一个分类,如: 按对象对应关系分: 一对一 多对一/一对多 多对 ...

  3. web进修之—Hibernate 懒加载(6)

    关于懒加载 在关系数据库设计的时候,我们很多时候把表之间的关系设置为强关联(使用外键进行约束),在Hibernate中利用对象的包含关系进行维护(HIbernate本身就是面向对象的数据库操作模式), ...

  4. web进修之—Hibernate 继承映射(5)

    先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类:   view plaincopy to clipboardprint p ...

  5. web进修之—Hibernate 类型(4)

    本片包含Hibernate的两种类型的简单介绍和集合类型的映射. Hibernate中的两种类型: Entity 自己掌控自己的生命周期,比如Person有addrss属性(关联到另外一张表).age ...

  6. weblogic10异常:org.hibernate.hql.ast.HqlToken

    转自:http://www.programgo.com/article/68682994452/ 在做查询的时候,报出  org.hibernate.QueryException: ClassNotF ...

  7. java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode(尼玛,蛋疼的错误)

    java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode   \-[M ...

  8. WebLogic使用SSH架构部署遇到org.hibernate.hql.internal.ast.HqlTok

    其实这个问题在以前就遇到过,当时解决了,但今天在部署一个测试轻应用的时候一直没有想起来,特此记录一下. 这个问题出现在使用WebLogic(我使用的是10.3.5版本)发布SSH架构的应用.在操作数据 ...

  9. weblogic 下异常 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

    项目之前在 Tomcat 环境下一直都正常运行,今天应客户要求需要迁移到 webLogic 10.3.6 下, 部署后竟然抛出了 org.hibernate.QueryException: Class ...

随机推荐

  1. sequence测试中的使用

    1. create sequence : create sequence TEST_SEQUENCE minvalue 1 maxvalue 1000000000 start with 1 incre ...

  2. Kinfu配置指南

    Kinfu配置指南 欢迎加入Kinfu讨论群:563741937 写给准备配置pcl-kinfu的同学,这个是我的配置方法,已经能用的请无视. 我的配置:Windows7,VS2010(用这个的是不是 ...

  3. 如何往有自增标识字段的表插入数据时,同时给自增标识字段插入值呢,在Inset Into语句前后加上SQL语句:SET IDENTITY_INSERT TableName ON和SET IDENTITY_INSERT TableName OFF

    当要往有设置自增标识字段的表插入数据,并希望同时设置好自增字段的值时,可以在insert into 的SQL语句前后分别加上一句sql语句,SET IDENTITY_INSERT TableName  ...

  4. Missing initializer in const declaration

    这是一个 JS 的报错. 如果你要声明一个常量,必须要赋初值.否则就会报错. 比如这样就可以触发这个报错: const foo; 或者 const bar = xxx; 等号右侧由于拼写失误导致的js ...

  5. one-to-one 一对一映射关系(转 wq群)

    2.配置对应的xml配置文件 person的配置文件 idCard的配置文件 idCard的配置文件  3.测试 运行测试程序后,控制台输出两条语句

  6. git的命令行操作

    1.初始化本地的git仓库git init,代码存放在这里,git会自动对我们的代码进行管理备份. 2.设置用户信息,设置用户名:git config --global user.name " ...

  7. Python环境安装及IDE介绍

    因为最近时间比较松散,公司的业务也不多,所以想趁机赶紧投入到人工智能的学习大业当中.经过多次比较,看到目前市面上还是使用Python做为基础语言较多,进儿学习算法.人工智能组件.机器学习.数据挖掘等课 ...

  8. 学习笔记----php环境配置

    Php开发环境自定义搭建 (万事开头难) 第一步:Apache安装(httpd-2.4.37-win64-VC15.zip) 下载已编译apache安装包:Apachelounge官方下载地址:htt ...

  9. 6 week work 1

    CSS单位 em and rem: are often used to create scalable layouts, which maintain the vertical rhythm of t ...

  10. css格式比较及选择器类型总结

    在前端入门的前三天把网页制作过程中常用的一些标签和属性都认识和练习了一遍,能够做出简单模块的框架.就像老师说的网页制作就像建一栋大楼,html是砖和水泥,css是精装,js是完善各个功能.现在就开始进 ...