hql查询语句(hibernate)
2012-07-27 16:55
162 查看
.
1.from子句
from Person
表明从Person持久化类中选出全部的实例。
推荐:from Personas p
2.select子句
select p.name from Person as p
select p.name.firstName from Person as p
select new list(p.name, p.address) fromPerson as p
select new ClassTest(p.name, p.address)from Person as p (有前提)
select p.name as personName from Person asp
select new map(p.name as personName) fromPerson as p (与new map()结合更普遍)
3.统计函数查询:
1: count() 统计记录的条数
2: min() 求最小值
3: max() 求最大值
4: sum() 求和
4: avg() 求平均值
//取得Student的数量
Query query=session.createQuery("select count(*) fromStudent")
//avg()取得Student平均年龄
Query query=session.createQuery("select avg(s.age) from Student ass")
//upper()方法将字符串转为大写
Query query=session.createQuery("select upper(s.name) from Studentas s")
//去除重复行distinct
Query query=session.createQuery("select distinct s.age from Studentas s")
select count(*) from Person
select max(p.age) from Person as p
select p.name || "" || p.addressfrom Person as p
4.多态查询
from Person as p
from java.lang.Object o
from Named as n
5.where子句
from Person where name like"tom%"
from Person as p where p.name like"tom%"
from Cat cat where cat.mate.name like"kit%"
select * from cat_table as table1 cat_table astable2 where table1.mate =
table2.id and table1.name like"kit%"
from Foo foo wherefoo.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'
andperson.id.medicareNumber = 123456
from Account as account
where account.owner.id.country = 'AU'
andaccount.owner.id.medicareNumber = 123456
from Cat cat where cat.class = DomesticCat
from Account as a wherea.person.name.firstName like "dd%" // 正确
from Account as a where a.person.name like"dd%" // 错误
6.表达式
=, <>, >, <, >=, <=,between, not between, in, not in, is, like等。
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name in('Foo', 'Bar', 'Baz')
from DomesticCat cat where cat.name notbetween 'A' and 'B'
from DomesticCat cat where cat.name not in('Foo', 'Bar', 'Baz')
from DomesticCat cat where cat.name is null
from Person as p where p.address is notnull
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 wheremaxelement(cal.holidays) > current date
from Order order wheremaxindex(order.items) > 100
from Order order whereminelement(order.items) > 10000
//操作集合元素
select mother from Cat as mother, Cat askit
where kit in elements(foo.kittens)
//p的name属性等于集合中某个元素的name属性
select p from NameList list, Person p
where p.name = some elements(list.names)
//操作集合元素
from Cat cat where existselements(cat.kittens)
from Player p where 3 > allelements(p.scores)
from Show show where 'fizard' inindices(show.acts)
//items是有序集合属性,items[0]代表第一个元素
from Order order where order.items[0].id =1234
//holidays是map集合属性,holidays[nationalday]是代表其中第一个元素
select person from Person person, Calendarcalendar
where calendar.holidays['national day'] =person.birthDay
andperson.nationality.calendar = calendar
//下面同时使用list集合和map集合属性
select item from Item item, Order order
whereorder.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
select cust
from Product prod,
Store store
inner join store.customers cust
where prod.name = 'widget'
andstore.location.name in ['Melbourne', 'Sydney']
andprod = all elements(cust.currentOrder.lineItems)
SELECT cust.name, cust.address, cust.phone,cust.id, cust.current_order
FROM customers cust,
stores store,
locations loc,
store_customers sc,
product prod
WHERE prod.name = 'widget'
ANDstore.loc_id = loc.id
ANDloc.name IN ('Melbourne', 'Sydney')
ANDsc.store_id = store.id
ANDsc.cust_id = cust.id
ANDprod.id = ALL(
SELECT item.prod_id
FROMline_items item, orders o
WHERE item.order_id = o.id
ANDcust.current_order = o.id
)
like语句:
String hql = "from Users u where u.userName=? and u.userPass=? andu.userInfo like ?";
Object param[] = { userName, password, '%'+info+'%' };
7.order by子句
from Person as p
order by p.name, p.age
from Person as p
order by p.name asc, p.age desc
8.group by子句
select cat.color, sum(cat.weight),count(cat)
from Cat cat
group by cat.color
//select后出现的id处出现在group by之后,而name属性则出现在聚集函数中
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id
select cat.color, sum(cat.weight),count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY,eg.Color.BLACK)
select cat
from Cat cat
join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc,sum(kitten.weight) desc
9.子查询:
all 表示所有记录
any 便是所有记录中的任意一条
somy 与any用法一样
in 等价于any
exists 表示子查询至少要返回一条数据
all:
from Team t where 22<all(select s.age from Student s)
from Team t where all(select s.age from t.student s)>22
from Cat as fatcat
where fatcat.weight > (selectavg(cat.weight) from DomesticCat cat)
from Cat as cat
where not (cat.name, cat.color) in (
select cat.name, cat.color from DomesticCatcat
)
10.fetch关键字
from Person as p join p.scores
from Document fetch all properties order byname
from Document doc fetch all propertieswhere lower(doc.name) like '%cat%'
11.查询链接:
与SQL查询一样,hql也支持连接查询,如内连接,外连接和交叉连接.支持的链接类型是从ANSISQL中借鉴来的.
1: inner jion (内连接)
2: left outer join (左外连接)
3: right outer join(右外连接)
4: full join(全连接--不常用)
inner jion 可以简写为join.
正常情况下必须要建关联。
1.from子句
from Person
表明从Person持久化类中选出全部的实例。
推荐:from Personas p
2.select子句
select p.name from Person as p
select p.name.firstName from Person as p
select new list(p.name, p.address) fromPerson as p
select new ClassTest(p.name, p.address)from Person as p (有前提)
select p.name as personName from Person asp
select new map(p.name as personName) fromPerson as p (与new map()结合更普遍)
3.统计函数查询:
1: count() 统计记录的条数
2: min() 求最小值
3: max() 求最大值
4: sum() 求和
4: avg() 求平均值
//取得Student的数量
Query query=session.createQuery("select count(*) fromStudent")
//avg()取得Student平均年龄
Query query=session.createQuery("select avg(s.age) from Student ass")
//upper()方法将字符串转为大写
Query query=session.createQuery("select upper(s.name) from Studentas s")
//去除重复行distinct
Query query=session.createQuery("select distinct s.age from Studentas s")
select count(*) from Person
select max(p.age) from Person as p
select p.name || "" || p.addressfrom Person as p
4.多态查询
from Person as p
from java.lang.Object o
from Named as n
5.where子句
from Person where name like"tom%"
from Person as p where p.name like"tom%"
from Cat cat where cat.mate.name like"kit%"
select * from cat_table as table1 cat_table astable2 where table1.mate =
table2.id and table1.name like"kit%"
from Foo foo wherefoo.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'
andperson.id.medicareNumber = 123456
from Account as account
where account.owner.id.country = 'AU'
andaccount.owner.id.medicareNumber = 123456
from Cat cat where cat.class = DomesticCat
from Account as a wherea.person.name.firstName like "dd%" // 正确
from Account as a where a.person.name like"dd%" // 错误
6.表达式
=, <>, >, <, >=, <=,between, not between, in, not in, is, like等。
from DomesticCat cat where cat.name between 'A' and 'B'
from DomesticCat cat where cat.name in('Foo', 'Bar', 'Baz')
from DomesticCat cat where cat.name notbetween 'A' and 'B'
from DomesticCat cat where cat.name not in('Foo', 'Bar', 'Baz')
from DomesticCat cat where cat.name is null
from Person as p where p.address is notnull
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 wheremaxelement(cal.holidays) > current date
from Order order wheremaxindex(order.items) > 100
from Order order whereminelement(order.items) > 10000
//操作集合元素
select mother from Cat as mother, Cat askit
where kit in elements(foo.kittens)
//p的name属性等于集合中某个元素的name属性
select p from NameList list, Person p
where p.name = some elements(list.names)
//操作集合元素
from Cat cat where existselements(cat.kittens)
from Player p where 3 > allelements(p.scores)
from Show show where 'fizard' inindices(show.acts)
//items是有序集合属性,items[0]代表第一个元素
from Order order where order.items[0].id =1234
//holidays是map集合属性,holidays[nationalday]是代表其中第一个元素
select person from Person person, Calendarcalendar
where calendar.holidays['national day'] =person.birthDay
andperson.nationality.calendar = calendar
//下面同时使用list集合和map集合属性
select item from Item item, Order order
whereorder.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
select cust
from Product prod,
Store store
inner join store.customers cust
where prod.name = 'widget'
andstore.location.name in ['Melbourne', 'Sydney']
andprod = all elements(cust.currentOrder.lineItems)
SELECT cust.name, cust.address, cust.phone,cust.id, cust.current_order
FROM customers cust,
stores store,
locations loc,
store_customers sc,
product prod
WHERE prod.name = 'widget'
ANDstore.loc_id = loc.id
ANDloc.name IN ('Melbourne', 'Sydney')
ANDsc.store_id = store.id
ANDsc.cust_id = cust.id
ANDprod.id = ALL(
SELECT item.prod_id
FROMline_items item, orders o
WHERE item.order_id = o.id
ANDcust.current_order = o.id
)
like语句:
String hql = "from Users u where u.userName=? and u.userPass=? andu.userInfo like ?";
Object param[] = { userName, password, '%'+info+'%' };
7.order by子句
from Person as p
order by p.name, p.age
from Person as p
order by p.name asc, p.age desc
8.group by子句
select cat.color, sum(cat.weight),count(cat)
from Cat cat
group by cat.color
//select后出现的id处出现在group by之后,而name属性则出现在聚集函数中
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id
select cat.color, sum(cat.weight),count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY,eg.Color.BLACK)
select cat
from Cat cat
join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc,sum(kitten.weight) desc
9.子查询:
all 表示所有记录
any 便是所有记录中的任意一条
somy 与any用法一样
in 等价于any
exists 表示子查询至少要返回一条数据
all:
from Team t where 22<all(select s.age from Student s)
from Team t where all(select s.age from t.student s)>22
from Cat as fatcat
where fatcat.weight > (selectavg(cat.weight) from DomesticCat cat)
from Cat as cat
where not (cat.name, cat.color) in (
select cat.name, cat.color from DomesticCatcat
)
10.fetch关键字
from Person as p join p.scores
from Document fetch all properties order byname
from Document doc fetch all propertieswhere lower(doc.name) like '%cat%'
11.查询链接:
与SQL查询一样,hql也支持连接查询,如内连接,外连接和交叉连接.支持的链接类型是从ANSISQL中借鉴来的.
1: inner jion (内连接)
2: left outer join (左外连接)
3: right outer join(右外连接)
4: full join(全连接--不常用)
inner jion 可以简写为join.
正常情况下必须要建关联。
相关文章推荐
- hibernate--HQL查询语句(1)
- Hibernate常用查询语句(hql)
- hibernate--HQL查询语句(二)-----分组、排序
- hibernate 中HQL语句查询学习
- 在hibernate中,对数据库的增删改操作以及HQL查询与QBC查询的语句的编写语法
- hibernate--HQL查询语句(2)
- hibernate的hql查询语句总结
- hibernate的hql查询语句总结
- Hibernate的HQL查询及原生sql查询语句写法
- Hibernate的HQL简单查询语句
- hibernate的hql查询语句总结
- Eclipse使用hibernate进行HQL语句查询时,速度慢的原因之一
- hibernate根据条件动态组装sql/hql语句(仿ibatis动态查询语句功能)
- Hibernate学习之hql查询语句
- hibernate根据条件动态组装sql/hql语句(仿ibatis动态查询语句功
- Hibernate查询语句HQL
- Hibernate HQL查询语句总结
- hibernate查询语句---HQL
- Hibernate使用hql语句查询
- hibernate的hql查询语句总结