public void testCriteria(){
Criteria criteria = session.createCriteria(User.
class);
User user =
new User();
user.setAge(10);
NamePk namePk =
new NamePk();
namePk.setFirstName(
"zhao");
namePk.setLastName(
"guoyu");
user.setNamePk(namePk);
user.setEmail(
"zhao@163.com");
criteria.add(Example.create(user));
criteria.list();
}
select this_.firstname as firstname0_0_,
this_.lastname as lastname0_0_,
this_.age as age0_0_,
this_.address as address0_0_,
this_.tel as tel0_0_,
this_.zipcode as zipcode0_0_,
this_.email as email0_0_
from t_user this_
where (this_.age = ? and this_.email = ?)
public void testCriteria2(){
Criteria criteria = session.createCriteria(User.
class);
criteria.add(Expression.le(
"age", 20));
criteria.list();
}
select this_.firstname as firstname0_0_,
this_.lastname as lastname0_0_,
this_.age as age0_0_,
this_.address as address0_0_,
this_.tel as tel0_0_,
this_.zipcode as zipcode0_0_,
this_.email as email0_0_
from t_user this_
where this_.age <= ?
public void testCriteria3(){
Criteria criteria = session.createCriteria(User.
class);
// Criteria addrCriteria = criteria.createCriteria("address"); // addrCriteria.add(Expression.eq("tel", "189733300")); Address address =
new Address();
address.setTel(
"112");
criteria.add(Restrictions.eq(
"address",address));
criteria.list();
}
select this_.firstname as firstname0_0_,
this_.lastname as lastname0_0_,
this_.age as age0_0_,
this_.address as address0_0_,
this_.tel as tel0_0_,
this_.zipcode as zipcode0_0_,
this_.email as email0_0_
from t_user this_
where (this_.address = ? and this_.tel = ? and this_.zipcode = ?)
public void testCriteeria3(){
Criteria criteria = session.createCriteria(User.
class);
criteria.setFirstResult(100);
criteria.setMaxResults(3);
criteria.list();
}
select *
from (select row_.*, rownum rownum_
from (select this_.firstname as firstname0_0_,
this_.lastname as lastname0_0_,
this_.age as age0_0_,
this_.address as address0_0_,
this_.tel as tel0_0_,
this_.zipcode as zipcode0_0_,
this_.email as email0_0_
from t_user this_) row_
where rownum <= ?)
where rownum_ > ?
public void testCriteria4(){
Criteria criteria = session.createCriteria(User.
class);
criteria.setProjection(Projections.groupProperty(
"age"));
criteria.list();
}
select this_.age as y0_ from t_user this_ group by this_.age
public void testCriteria5(){
Criteria criteria = session.createCriteria(User.
class);
criteria.setProjection(Projections.avg(
"age"));
criteria.list();
}
select avg(this_.age) as y0_ from t_user this_
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/246706,如需转载请自行联系原作者