大家知道在Hibernate Framework中提供了三种查询数据的方式
Hibernate Query Language (HQL)
它是ANSI SQL的最小OO Dialect例
session
createQuery(
from Category c where c
name like
Laptop%
);
entityManagercreateQuery(select c from Category c where cname like Laptop%);
Criteria query
它是HQL查询的延伸提供了一些高级查询功能例
session
createCriteria(Category
class)
add( Restrictionslike(name Laptop%) );
Native SQL query :
session
createSQLQuery(
select {c*} from CATEGORY {c} where NAME like Laptop%
)addEntity(c Categoryclass);
最近我在项目中经常遇到Hibernate数据查询的任务我对一些我遇到的较难的课题举例进行说明
《表述SELECT》
Criteria crit = session
createCriteria(User
class)
setProjection( ProjectionsprojectionList()
add( Projectionsproperty(lastname))
add( Projectionsproperty(firstname))
list();
《表述WHERE》
Criteria crit = session
createCriteria(User
class)
add(Restrictionseq(email foo@hibernateorg))
uniqueResult();
《表述GROUP》
Criteria crit = session
createCriteria(User
class)
setProjection( ProjectionsprojectionList()
add( ProjectionsgroupProperty(lastname))
add( ProjectionsgroupProperty(firstname))
list()
《表述ORDER》
Criteria crit = session
createCriteria(User
class)
addOrder( Orderasc(lastname) )
addOrder( Orderasc(firstname) )
list();
[] []