1.from子句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
表明从Person持久化类中选出全部的实例。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
推荐:from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2.select子句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select p.name from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select p.name.firstName from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select new list(p.name, p.address) from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select new ClassTest(p.name, p.address) from Person as p (有前提)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select p.name as personName from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select new map(p.name as personName) from Person as p (与new map()结合更普遍)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3.聚集函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
avg,count,max,min,sum
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select count(*) from Person
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select max(p.age) from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select p.name || "" || p.address from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4.多态查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from java.lang.Object o
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Named as n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
5.where子句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person where name like "tom%"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p where p.name like "tom%"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where cat.mate.name like "kit%"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select * from cat_table as table1 cat_table as table2 where table1.mate =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
table2.id and table1.name like "kit%"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Foo foo where foo.bar.baz.customer.address.city like "fuzhou%"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat, Cat rival where cat.mate = rival.mate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cat, mate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat, Cat mate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where cat.mate = mate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat as cat where cat.id = 123
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat as cat where cat.mate.id = 69
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as person
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where person.id.country = 'AU'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
and person.id.medicareNumber = 123456
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Account as account
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where account.owner.id.country = 'AU'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
and account.owner.id.medicareNumber = 123456
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where cat.class = DomesticCat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Account as a where a.person.name.firstName like "dd%" // 正确
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Account as a where a.person.name like "dd%" // 错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
6.表达式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from DomesticCat cat where cat.name between 'A' and 'B'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from DomesticCat cat where cat.name in ('Foo', 'Bar', 'Baz')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from DomesticCat cat where cat.name not between 'A' and 'B'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from DomesticCat cat where cat.name not in ('Foo', 'Bar', 'Baz')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from DomesticCat cat where cat.name is null
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p where p.address is not null
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
true 1, false 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where cat.alive = true
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where cat.kittens.size > 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where size(cat.kittens) > 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Calendar cal where maxelement(cal.holidays) > current date
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Order order where maxindex(order.items) > 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Order order where minelement(order.items) > 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//操作集合元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select mother from Cat as mother, Cat as kit
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where kit in elements(foo.kittens)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//p的name属性等于集合中某个元素的name属性
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select p from NameList list, Person p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where p.name = some elements(list.names)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//操作集合元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat where exists elements(cat.kittens)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Player p where 3 > all elements(p.scores)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Show show where 'fizard' in indices(show.acts)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//items是有序集合属性,items[0]代表第一个元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Order order where order.items[0].id = 1234
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//holidays是map集合属性,holidays[national day]是代表其中第一个元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select person from Person person, Calendar calendar
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where calendar.holidays['national day'] = person.birthDay
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
and person.nationality.calendar = calendar
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//下面同时使用list集合和map集合属性
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select item from Item item, Order order
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where order.items[order.deliveredItemIndices[0]] = item and order.id = 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select item from Item item, Order order
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where order.items[maxindex(order.items)] = item and order.id = 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select item from Item item, Order order
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where order.items[size(order.items) - 1] = item
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cust
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Product prod,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Store store
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
inner join store.customers cust
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where prod.name = 'widget'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
and store.location.name in ['Melbourne', 'Sydney']
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
and prod = all elements(cust.currentOrder.lineItems)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FROM customers cust,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
stores store,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
locations loc,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
store_customers sc,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
product prod
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHERE prod.name = 'widget'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND store.loc_id = loc.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND loc.name IN ('Melbourne', 'Sydney')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND sc.store_id = store.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND sc.cust_id = cust.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND prod.id = ALL(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT item.prod_id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FROM line_items item, orders o
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHERE item.order_id = o.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND cust.current_order = o.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
7.order by子句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
order by p.name, p.age
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
order by p.name asc, p.age desc
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
8.group by子句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cat.color, sum(cat.weight), count(cat)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
group by cat.color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//select后出现的id处出现在group by之后,而name属性则出现在聚集函数中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select foo.id, avg(name), max(name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Foo foo join foo.names name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
group by foo.id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cat.color, sum(cat.weight), count(cat)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
group by cat.color
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
join cat.kittens kitten
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
group by cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
having avg(kitten.weight) > 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
order by count(kitten) asc, sum(kitten.weight) desc
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
9.子查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat as fatcat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where fatcat.weight > (select avg(cat.weight) from DomesticCat cat)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Cat as cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
where not (cat.name, cat.color) in (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cat.name, cat.color from DomesticCat cat
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10.fetch关键字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Person as p join p.scores
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Document fetch all properties order by name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from Document doc fetch all properties where lower(doc.name) like '%cat%'
11.执行hql
String hql ="update ContentReply contentReply set active = false where contentReply.id =:replyId";
getSession().createQuery(hql).setString("replyId", replyId).executeUpdate();