概述

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. kali自定义分辨率(1920*1080)

    运行一下两行代码: xrandr --newmode -hsync +vsync xrandr --addmode Virtual1 "1920x1080_60.00"

  2. Mac- appium 环境配置

    一: 下载    appium : https://bitbucket.org/appium/appium.app/downloads/ 下载安装完,打开appium,安全性与隐私设置  检测提示: ...

  3. 微信小程序的自定义插件

    第一步,创建一个页面和普通页面一样 第二不,在这个页面上进行对json配置, "component":true 第三不在需要插入的页面中进行设置 插入标签 <dialog i ...

  4. ROS学习笔记(一) : 入门之基本概念

    目录 基本概念 1. Package 2. Repositories 3. Computation Graph 4. Node 5. Master 6. Message 7. Topic 8. Ser ...

  5. 基于jmeter的性能测试平台(一)分布式jmeter搭建

    (1)概述 一台windows虚拟机作为controller,3台Linux虚拟机作为agent. 第一步是在所有虚拟机上安装JDK,版本最好是一样的,然后就是下载安装jmeter,网上资料很多这里不 ...

  6. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

  7. 《Tornado介绍》—— 读后总结

  8. 透彻讲解,Java线程的6种状态及切换

    Java中线程的状态分为6种. 1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法.2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running) ...

  9. Android JNI 学习(七):Accessing Fields Api

    一.GetFieldID jfieldIDGetFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig); 返回类的实 ...

  10. java小白之面向对象

    面向对象 面相对象(oop)和面向过程(pop)通常一起说,一个是更加关注过程,事力亲为,而面向对象更加注重结果,所以说,面向对象更加是一种思想,它贯穿整个java,以上帝视角来看整个功能需求,简化开 ...