Hibernate查询语句与Callback使用方法.doc
文本预览下载声明
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.统计函数查询:
1: count() 统计记录的条数
2: min() 求最小值
3: max() 求最大值
4: sum() 求和
4: avg() 求平均值
//取得Student的数量
Query query=session.createQuery(select count(*) from Student)
//avg()取得Student平均年龄
Query query=session.createQuery(select avg(s.age) from Student as s)
//upper()方法将字符串转为大写
Query query=session.createQuery(select upper() from Student as s)
//去除重复行distinct
Query query=session.createQuery(select distinct s.age from Student as s)
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 java.lang.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.表达式
=, , , , =, =, between, not between, in, not in, is, like等。
from DomesticCat cat where between A and B
from DomesticCat cat where in (Foo, Bar, Baz)
from DomesticCat cat where not
显示全部