概述

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

    自己总结一下Java文件的读取类似数组数据的方法,自己可以快速查看. 一.规整化数据: 对于数组数据是一一对应的情况 ArrayList<String> arrayList = new A ...

  2. 冒泡排序&&选择排序

    package cn.lijun.demo;//冒泡排序public class Test5 { public static void main(String[] args) { int[] arr ...

  3. MyBatis generator配置 overwrite 文件覆盖失效

    工具:IDEA.jdk1.8.mysql 底部有解决方法! pom.xml配置 <plugins> <!--Mybatis自动代码插入--> <plugin> &l ...

  4. JAVA 8 主要新特性 ----------------(七)新时间日期 API -----LocalDateTime

    一.LocalDateTime简介 二.实战讲解 LocalDateTime localDateMax = LocalDateTime.MAX; System.out.println("lo ...

  5. webpack踩坑--webpack 2.x升级至4.x

    一.安装webpack-cli,webpack@4.26.1 1.npm install webpack-cli -D 2.npm install webpack@4.26.1 -D 二.踩坑 执行n ...

  6. Sql Server常用数据库函数小结

    查询验证存储过程是否存在 SELECT name FROM sysobjects WHERE  xtype = 'P' and name = '{0}' ----------------------- ...

  7. 收集几个html和element-ui的录入控件

    我希望有一个控件去显示或输入账本的金额 先做一些资料收集,也希望大家给个建议 输入银行账号会设置每四位添加一个空格 https://blog.csdn.net/wkx18330698534/artic ...

  8. <笔记>TP5的分页传递额外参数

    默认生成的分页只有page一个参数,若需要提供额外的参数才能访问分页(例如查询结果的分页,需要传入查询关键字的参数才能显示结果),则需要设置额外参数query

  9. vscode调试golang环境搭建及配置

    准备VSCode 在官网下载最新版的VSCode: 安装Golang插件 打开扩展面板 VSCode->查看->扩展 找到Go插件 在搜索框里输入Go, 找到第二行写有 Rich Go l ...

  10. ubuntu16 mysql在线安装

    输入"sudo apt-get update"-->回车-->"输入root用户的密码"-->回车: 输入"sudo apt-get ...