1. 复合属性的查询
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();
}
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();
}
生成的SQL
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 = ?)
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 = ?)
2.
public
void testCriteria2(){
Criteria criteria = session.createCriteria(User. class);
criteria.add(Expression.le( "age", 20));
criteria.list();
}
Criteria criteria = session.createCriteria(User. class);
criteria.add(Expression.le( "age", 20));
criteria.list();
}
生成的SQL
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 <= ?
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 <= ?
3.
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();
}
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();
}
生成的SQL:
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 = ?)
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 = ?)
4.
public
void testCriteeria3(){
Criteria criteria = session.createCriteria(User. class);
criteria.setFirstResult(100);
criteria.setMaxResults(3);
criteria.list();
}
Criteria criteria = session.createCriteria(User. class);
criteria.setFirstResult(100);
criteria.setMaxResults(3);
criteria.list();
}
生成的sql:
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_ > ?
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_ > ?
5.
public
void testCriteria4(){
Criteria criteria = session.createCriteria(User. class);
criteria.setProjection(Projections.groupProperty( "age"));
criteria.list();
}
Criteria criteria = session.createCriteria(User. class);
criteria.setProjection(Projections.groupProperty( "age"));
criteria.list();
}
生成的sql:
select this_.age as y0_ from t_user this_ group by this_.age
6.
public
void testCriteria5(){
Criteria criteria = session.createCriteria(User. class);
criteria.setProjection(Projections.avg( "age"));
criteria.list();
}
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,如需转载请自行联系原作者