hibernate查询方式举例
1 单个对象
a) Query query=session.createQuery("from ConsortBean a where a.person.id=?");
query.setString(0,pb.getId());//参数从零开始
List list=query.list();
b) Query query=session.createQuery("from ConsortBean a where a.person.id=:id");
query.setString("id",pb.getId());
List list=query.list();
c) List list =session.find(
"from ConsortBean a where a.person.id=?",
pb.getId(),
Hibernate.STRING);
2 分页
int pagecount = 10;//每页数量
..
q =
session.createQuery(
"from FootPayRequestBean as fprb where fprb.state=? order by fprb.contractID");
q.setString(0, type);
q.setFirstResult(page * pagecount);//page从0开始
q.setMaxResults(pagecount);
l = q.list();
3 求count()
((Integer) session.iterate("select count(*) from ConsortBean").next() ).intValue();
4 联合查询
Query query=session.createQuery("select a from ConsortBean a,PersonBean b where a.person.id=b.id and b.name like ?");//对象别名
query.setString(0,"%管%");
List list=query.list();