大家知道在Hibernate Framework中提供了三種查詢數據的方式
Hibernate Query Language (HQL)
它是ANSI SQL的最小OO Dialect例
session
createQuery(
from Category c where c
name like
Laptop%
);
entityManager
createQuery(
select c from Category c where c
name like
Laptop%
);
Criteria query
它是HQL查詢的延伸提供了一些高級查詢功能例
session
createCriteria(Category
class)
add( Restrictions
like(
name
Laptop%
) );
Native SQL query :
session
createSQLQuery(
select {c
*} from CATEGORY {c} where NAME like
Laptop%
)
addEntity(
c
Category
class);
最近我在項目中經常遇到Hibernate數據查詢的任務我對一些我遇到的較難的課題舉例進行說明
《表述SELECT》
Criteria crit = session
createCriteria(User
class)
setProjection( Projections
projectionList()
add( Projections
property(
lastname
))
add( Projections
property(
firstname
))
list();
《表述WHERE》
Criteria crit = session
createCriteria(User
class)
add(Restrictions
eq(
email
foo@hibernate
org
))
uniqueResult();
《表述GROUP》
Criteria crit = session
createCriteria(User
class)
setProjection( Projections
projectionList()
add( Projections
groupProperty(
lastname
))
add( Projections
groupProperty(
firstname
))
list()
《表述ORDER》
Criteria crit = session
createCriteria(User
class)
addOrder( Order
asc(
lastname
) )
addOrder( Order
asc(
firstname
) )
list();
[] []
From:http://tw.wingwit.com/Article/program/Java/ky/201311/29154.html