博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web进修之—Hibernate HQL(7)
阅读量:6910 次
发布时间:2019-06-27

本文共 3307 字,大约阅读时间需要 11 分钟。

概述

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

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

简单查询语句

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

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

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

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

查询所有课程

view plain copy to clipboard print
  1. from Course  
from Course

查询课程名称为math的课程

view plain copy to clipboard print
  1. from Course as course where course.name='math'  
from Course as course where course.name='math'

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

view plain copy to clipboard print
  1. select count(*) from Course as course where course.name='math'  
select count(*) from Course as course where course.name='math'

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

view plain copy to clipboard print
  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降序

view plain copy to clipboard print
  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)

显式内连接:

view plain copy to clipboard print
  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:

view plain copy to clipboard print
  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是一对一的关系:

view plain copy to clipboard print
  1. from Course as course where course.subject.name='计算机科学'  
from Course as course where course.subject.name='计算机科学'

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

外连接

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

view plain copy to clipboard print
  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填充

view plain copy to clipboard print
  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),课程表对课程多对一,单向

view plain copy to clipboard print
  1. from Course as course, CourseTable as courseTable   
  2. where course.id=courseTable.course.id  
from Course as course, CourseTable as courseTablewhere course.id=courseTable.course.id

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

转载于:https://www.cnblogs.com/sunshine-2015/p/5372024.html

你可能感兴趣的文章
基于FormsAuthentication的用户、角色身份认证
查看>>
阿里云-DRDS(转)
查看>>
Lua协程-测试3
查看>>
009-java中常用的单个键值对
查看>>
微软为AJAX和jQuery类库提供CDN服务
查看>>
LuoYun 开源云计算平台软件 0.2 Beta 版本发布 — LinuxTOY
查看>>
约会你的私人定制
查看>>
MD5 加密的两种方法
查看>>
Winfrom 控件移位绘制垂直、水平线
查看>>
如何开启to 日志
查看>>
android和iOS平台的崩溃捕获和收集
查看>>
sql执行计划[Oracle] 变量绑定
查看>>
[Swift A] - Welcome to Swift
查看>>
Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution
查看>>
python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码...
查看>>
SQL Server的镜像是基于物理块变化的复制 镜像Failover之后数据的预热问题
查看>>
STL 清除模板容器 clear.h
查看>>
gradle多模块开发(转)
查看>>
Linux 网络编程一(TCP/IP协议)
查看>>
ckplayer 实现
查看>>