HQL语句
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
HQL语句的语法
1.from子句
from Person
表明从Person持久化类中选出全部的实例。
推荐:from Person as p
2.select子句
select from Person as p
select .firstName from Person as p
select new list(, p.address) from Person as p
select new ClassTest(, p.address) from Person as p (有前提)
select as personName from Person as p
select new map( as personName) from Person as p (与new map()结合更普遍)
3.聚集函数
avg,count,max,min,sum
select count(*) from Person
select max(p.age) from Person as p
select || "" || p.address from Person as p
4.多态查询
from Person as p
from ng.Object o
from Named as n
5.where子句
from Person where name like "tom%"
from Person as p where like "tom%"
from Cat cat where like "kit%"
select * from cat_table as table1 cat_table as table2 where table1.mate =
table2.id and like "kit%"
from Foo foo where foo.bar.baz.customer.address.city like "fuzhou%"
from Cat cat, Cat rival where cat.mate = rival.mate
select cat, mate
from Cat cat, Cat mate
where cat.mate = mate
from Cat as cat where cat.id = 123
from Cat as cat where cat.mate.id = 69
from Person as person
where person.id.country = 'AU'
and person.id.medicareNumber = 123456
from Account as account
where account.owner.id.country = 'AU'
and account.owner.id.medicareNumber = 123456
from Cat cat where cat.class = DomesticCat
from Account as a where .firstName like "dd%" // 正确
from Account as a where like "dd%" // 错误
6.表达式
from DomesticCat cat where between 'A' and 'B'
from DomesticCat cat where in ('Foo', 'Bar', 'Baz')
from DomesticCat cat where not between 'A' and 'B'
from DomesticCat cat where not in ('Foo', 'Bar', 'Baz')
from DomesticCat cat where is null
from Person as p where p.address is not null
true 1, false 0
from Cat cat where cat.alive = true
from Cat cat where cat.kittens.size > 0
from Cat cat where size(cat.kittens) > 0
from Calendar cal where maxelement(cal.holidays) > current date
from Order order where maxindex(order.items) > 100
from Order order where minelement(order.items) > 10000
//操作集合元素
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
//p的name属性等于集合中某个元素的name属性
select p from NameList list, Person p
where = some elements(s)
//操作集合元素
from Cat cat where exists elements(cat.kittens)
from Player p where 3 > all elements(p.scores)
from Show show where 'fizard' in indices(show.acts)
//items是有序集合属性,items[0]代表第一个元素
from Order order where order.items[0].id = 1234
//holidays是map集合属性,holidays[national day]是代表其中第一个元素
select person from Person person, Calendar calendar
where calendar.holidays['national day'] = person.birthDay
and person.nationality.calendar = calendar
//下面同时使用list集合和map集合属性
select item from Item item, Order order
where order.items[order.deliveredItemIndices[0]] = item and order.id = 11 select item from Item item, Order order
where order.items[maxindex(order.items)] = item and order.id = 11
select item from Item item, Order order
where order.items[size(order.items) - 1] = item