Hibernate的HQL基础

本文介绍了HQL查询语言的使用技巧,包括大小写敏感性、关联查询、子查询等高级特性,并详细解释了select子句、where子句的应用,以及字符串、日期等内置函数的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a)大小写敏感问题:只对类和属性区分大小写。
b)关联:
(inner) join
left (outer) join
right (outer) join
full (outer) join

with
from Emp e join Dept d on e.deptId = d.id
with e.name is not null

fetch:把所关联的对象一同取出来
from Emp e join fetch e.dept
select e.dept from Emp e

from Emp e
where e.dept.company.group.address.city is not null

1) select子句:
select u.name, u.score from User u;
select new User(u.name, u.score) from User u;
select new List(u.name, u.score) from User u;
select new Map(u.name as uname, u.score as uscore) from User u;

2)命名参数
形式 —— “:paraname”
query.setParameter("paraname", "z");

3)where子句
a、比较运算:可以比较对象
from Emp e1, Emp e2
where e1.dept = e2.dept;
b)内置class属性
from Product p where p.class = Book;
相当于 from Book;

4)内置函数
a)字符串:
连接 ‘a’ || ‘b’
upper(s)、lower(s)
trim(s)
length(s)
substring(s, start, len)
b)日期函数:
取当前日期(时间):current_date(),current_time(),current_timestamp()
处理日期字段的:year(date_field),month(date_field),day(date_field) ,hour(),minute(),second()
from Order o
whrere year(o.createDate) = 2010
c)类型转换函数:
str(age) str(createDate)
cast(age as string)
cast('2011-01-05' as date)

'2011-01-05 12:10:23'
d)条件分支——“if else”
case category when 1 then 'Book'
when 2 then 'DVD'
else 'unknown'
end

case when money_val <= 20000 then 'deptManager'
when money_val >= 20000 and money_val <= 50000 then 'fz'
else 'topleader'
end

5)子查询(嵌套查询)
from Cat fatcat
where fatcat.weight >
(select avg(weight) from Cat);

6)注意:某些底层数据库不支持某些特殊的功能。
如 having中聚集函数(sum)在某些数据库 (如MySql)是不支持的。

from Emp e
group by e.age
having avg(e.age) < 60
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值