概述

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. java多线程中注入Spring对象问题

    web应用中java多线程并发处理业务时,容易抛出NullPointerException. 原因: 线程中的Spring Bean没有被注入.web容器在启动时,没有提前将线程中的bean注入,在线 ...

  2. 剑指offer PART 2

    剑指offer PART 2 书点击自取 提取码: njku 标签(空格分隔): 笔记 C++知识点: 1.面向对象的特性 2.构造函数 3.析构函数 4.动态绑定 5.常用的设计模式 6.UML图 ...

  3. 【翻译】Flume 1.8.0 User Guide(用户指南) Processors

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  4. python语言相关语法基础

    numpy系列import numpya = numpy.array([[1,2], [3,4]])b = numpy.array([[5,6], [7,8]])a*b>>>arra ...

  5. 2019浙江省赛B zoj4101 Element Swapping(推公式)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6003 题意 \(数组a通过交换一对数字,得到了b数组,给出x=\sum^n_{ ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学

    https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...

  7. 通过sqoop将hdfs数据导入MySQL

    简介:Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL ,Oracl ...

  8. cp/tar/用c语言编写程序 实现cp命令的效果

    1.cp (拷贝) 已存在文件路径  要拷贝的文件路径 实现cp命令的代码如下: #include <stdio.h> //因为要在命令中得到两个路径,所以要用到main函数的两个参数 i ...

  9. 【慕课网实战】九、以慕课网日志分析为例 进入大数据 Spark SQL 的世界

    即席查询普通查询 Load Data1) RDD DataFrame/Dataset2) Local Cloud(HDFS/S3) 将数据加载成RDDval masterLog = sc.textFi ...

  10. MVC+EF 多条件查询

    根据以前的做法是拼接sql语句,这会增加维护成本,因为sql语句里的内容不会报错,所以在使用ef的时候必须要抛弃拼接sql语句的习惯. 构建实例 List<vyw_user> list = ...