web进修之—Hibernate HQL(7)
概述
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的课程
from Course as course where course.name='math'
查询课程名称为math的课程总数
select count(*) from Course as course where course.name='math'
查询所有课程名称为math的课程,按id升序
- 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降序
- 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)
显式内连接:
- 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:
- select distinct course
- from Course as course
- inner join course.subjects as subject
- where subject.name='计算机科学'
select distinct course
from Course as course
inner join course.subjects as subject
where subject.name='计算机科学'
隐式内连接,如果课程和Subject是一对一的关系:
from Course as course where course.subject.name='计算机科学'
查询的结果直接就是Course对象的list
外连接
左外链接,包含两张表所有列,保留左面表的所有行,右面表没有的行null填充
- select distinct course
- from Course as course
- left join course.subjects as subject
- 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填充
- select distinct course
- from Course as course
- right join course.subjects as subject
- where subject.name='计算机科学'
select distinct course
from Course as course
right join course.subjects as subject
where subject.name='计算机科学'
多表查询
查询某张课程表(CourseTable)里面的所有课程(Course),课程表对课程多对一,单向
- from Course as course, CourseTable as courseTable
- 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)的更多相关文章
- web进修之—Hibernate起步(1)(2)
想开始写博客了,尝试了CSDN和cnblog之后还是觉得cnblog更加简洁.专注(不过cnblog不支持搬家),所以把刚刚写的两篇学习博客链接放在这儿,这样这个系列也算是完整了: web进修之—Hi ...
- web进修之—Hibernate 关系映射(3)
概述 Hibernate的关系映射是Hibernate使用的难点或者是重点(别担心,不考试哦~),按照不同的分类方式可以对这些映射关系做一个分类,如: 按对象对应关系分: 一对一 多对一/一对多 多对 ...
- web进修之—Hibernate 懒加载(6)
关于懒加载 在关系数据库设计的时候,我们很多时候把表之间的关系设置为强关联(使用外键进行约束),在Hibernate中利用对象的包含关系进行维护(HIbernate本身就是面向对象的数据库操作模式), ...
- web进修之—Hibernate 继承映射(5)
先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类: view plaincopy to clipboardprint p ...
- web进修之—Hibernate 类型(4)
本片包含Hibernate的两种类型的简单介绍和集合类型的映射. Hibernate中的两种类型: Entity 自己掌控自己的生命周期,比如Person有addrss属性(关联到另外一张表).age ...
- weblogic10异常:org.hibernate.hql.ast.HqlToken
转自:http://www.programgo.com/article/68682994452/ 在做查询的时候,报出 org.hibernate.QueryException: ClassNotF ...
- 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 ...
- WebLogic使用SSH架构部署遇到org.hibernate.hql.internal.ast.HqlTok
其实这个问题在以前就遇到过,当时解决了,但今天在部署一个测试轻应用的时候一直没有想起来,特此记录一下. 这个问题出现在使用WebLogic(我使用的是10.3.5版本)发布SSH架构的应用.在操作数据 ...
- weblogic 下异常 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
项目之前在 Tomcat 环境下一直都正常运行,今天应客户要求需要迁移到 webLogic 10.3.6 下, 部署后竟然抛出了 org.hibernate.QueryException: Class ...
随机推荐
- iBtais 多重嵌套循环
iBatis支持集合循环, 但是如何做到双重循环, 请见下例子 例子描述: 需要去三张结构相同的表中获取信息, 需要将信息拼合去重后返回 入参数据类型: Map<String,Object> ...
- python turtle库
turtle库初步 先看 https://www.cnblogs.com/chy8/p/9448606.html 一 turtle库介绍 turtle乌龟 import turtle from tur ...
- Mongo学习笔记
安装和开始 下载 MongoDB 参考:+MongoDB安装配置(Windows) +Mongo手册
- 运行纯PHP程序的时候,不应该加"?>"结束语
运行纯PHP程序的时候,不应该加"?>"结束语 只有在和HTML混搭的时候再用?>结束语! 理由:可以让php脚本更安全的运行, 比如在脚本末尾注入换行.空格等字符,没 ...
- Ubuntu 离线安装 docker
1.下载离线包,网址:https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/ 离线安装docker需要下载3个 ...
- C#中数组、ArrayList和List三者的区别 转
在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...
- 面向对象一 OOP与类
OOP面向对象 面向对象与面向过程 面向过程让计算机有步骤的顺次做一件事情,是一种过程化的叙事思维:面向对象是一种计算机世界里解决复杂软件工程的方法论,拆解问题复杂度,从人类思维角度提出解决问题的步骤 ...
- 常用maven仓库
常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...
- Redis-07.Spring Data整合Jedis
Spring整合Jedis 1.单实例(想节约时间的请直接拖到下面跳过1.2部分) step1:毫无疑问,首先需要在pom.xml中配置Jedis依赖 <dependency> <g ...
- 动态调试|Maccms SQL 注入分析(附注入盲注脚本)
0x01 前言 已经有一周没发表文章了,一个朋友叫我研究maccms的代码审计,碰到这个注入的漏洞挺有趣的,就在此写一篇分析文. 0x02 环境 Web: phpstudySystem: Window ...